python:多线程爬虫(美女照片)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python:多线程爬虫(美女照片)相关的知识,希望对你有一定的参考价值。

上编刚刚写的py,,而进度条不是很满意,而且 是单线程,所以修改为多线程,如果网络快,5分钟全部下载完全,该网站并发不好,而且经常访问不了,出现失败很正常。

只是学习py爬虫吧了。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
from os import path,makedirs
import re
from threading import Thread
from datetime import datetime
def trypicdir(picpath):
    if not path.exists(picpath):     #下载到的本地目录,路径不存在时创建一个
        makedirs(picpath)

#显示下载进度
def schedule(a,b,c):
    ‘‘‘‘‘
    a:已经下载的数据块
    b:数据块的大小
    c:远程文件的大小
    ‘‘‘
    per = 100.0 * a * b / c
    if per > 100 :
        per = 100
    #print (‘%.2f%%‘ % per)
    print (‘%.2f%%‘ % per),

#获取html源码
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html
#正则匹配分页
def findPage(html):
    myItems = re.findall(‘<span>(\d*)</span>‘, html, re.S)
    if myItems :
        return myItems.pop()
    else:
        return 0
#正则匹配列表
def findList(html):
    Items = re.findall(‘<span><a href="http://www.mzitu.com/(\d*)" target="_blank">(.*?)</a></span>‘, html, re.S)
    myItems=[]
    for i in Items:
        myItems.append((i[0]))
    return myItems

#下载图片
def downloadImg(url_pic,picpath):
    tmppic = re.findall("http:\/\/www.mzitu.com/(.*?)$",url_pic,re.S)[0]
    picfile = picpath+‘/%s.jpg‘ % ‘_‘.join(tmppic.split(‘/‘))
    html=getHtml(url_pic)
    myItems = re.findall(‘<p><a href="http:\/\/www.mzitu.com/.*?" ><img src="(.*?)" alt=".*?" /></a></p>‘,html,re.S)
    print (‘\n正在下载%s图片存储到本地%s.....‘%(url_pic,picfile))
    try:
        urllib.urlretrieve(myItems[0], picfile, schedule)
    except:
        print (‘下载%s图片存储到本地%s失败,请检查链接是否有问 ‘%(url_pic,picfile))

#单个美女连接下载
def getdowns(modelUrl,picpath):
    listHtml=getHtml(modelUrl)
    TotablNum=findPage(listHtml)
    if TotablNum != 0:
        for i in range(1,(int(TotablNum)+1)):
            downloadImg(url_pic=‘%s/%s‘%(modelUrl,i),picpath=picpath)
    else:
        downloadImg(url_pic=‘%s‘%(modelUrl),picpath=picpath)
‘‘‘
思路:
1、获取所有美女连接列表。
2、获取单个美女总连接数。
3、下载。
‘‘‘
if __name__ == ‘__main__‘:
    starttime=datetime.now()
    #picpath=r"F:\mypython3\9temp\9tmp_pic"
    picpath=‘/tmp/pic_tmp2‘
    trypicdir(picpath=picpath)
    #downloadImg(url_pic=‘http://www.mzitu.com/80942/43‘,picpath=picpath)
    listHtml = getHtml(‘http://www.mzitu.com/model‘)    #这是其中一个模块的url,可以添加不同的模块url从而达到整站爬取。
    listContent = findList(listHtml)
    #print ("listContent:",listContent)
    #多线程使用方法
    threads = []
    for i in listContent:
        threads.append(Thread(target=getdowns,args=(‘http://www.mzitu.com/%s‘%i,picpath)))
    for t in threads:
        #t.setDaemon(True)
        t.start()
    ‘‘‘
    #单线程使用方法.
    for m in listContent:
        getdowns(modelUrl=‘http://www.mzitu.com/%s‘%m,picpath=picpath)
    ‘‘‘
    endtime=datetime.now()
    print ("恭喜,所有美女图片已经下载完成,下载美女照片所花费时间为:%s秒."%(endtime-starttime).seconds)


本文出自 “都市布衣” 博客,请务必保留此出处http://sunday208.blog.51cto.com/377871/1881344

以上是关于python:多线程爬虫(美女照片)的主要内容,如果未能解决你的问题,请参考以下文章

Python 多线程爬虫

Python爬虫案例演示:Python多线程多进程协程

多线程——爬取以太坊Solidity智能合约代码的简约Python爬虫

python爬虫应用实战-如何爬取好看的小姐姐照片?

python爬虫应用实战-如何爬取好看的小姐姐照片?

Python爬虫编程思想(138):多线程和多进程爬虫--从Thread类继承