利用Python多线程爬虫——爬图片
程序功能大概就是爬取每个网页中的图片,并根据标题,分文件保存至指定目录,使用threading实现多线程。
主要流程为每访问一个网页,将此网页中的图片链接依次放入队列,根据图片数量依次开启下载线程,传入队列和编号,然后启动线程开始下载,主线程查询当前正在活动的线程数量,当数量为1的时候,即只剩主线程的时候,表示所有图片下载完毕,开始下一个网页。
class threadDownload(threading.Thread): def __init__(self,que,no): threading.Thread.__init__(self) self.que = que self.no = no def run(self): while True: if not self.que.empty(): saveImg(self.que.get(),'os'+str(self.no)+'.jpg') else: break
def saveToFile(FileName,srcList):
a=0
srcTuple = (srcList)
FileName = 'os'+FileName.strip()
res = mkdir(FileName)
if res == False:
return False
#os.mkdir(FileName)
os.chdir(FileName)
que = Queue.Queue()
for sl in srcList:
que.put(sl)
for a in range(0,srcList.__len__()):
threadD = threadDownload(que,a)
threadD.start()
#print threading.enumerate()
while threading.active_count() != 0:
if threading.active_count() == 1:
print FileName+" is Done"
return True
def saveImg(imgUrl,fileName):
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'
headers = {'User-Agent':user_agent}
try:
req = urllib2.Request(imgUrl,headers=headers)
res = urllib2.urlopen(req,timeout=5)
data = res.read()
except socket.timeout as e:
print "saveImgTimeOut"
return False
f = open(fileName,'wb')
f.write(data)
f.close()