python 多线程批量下载图片问题
本帖最后由 mingtian 于 2019-1-6 23:34 编辑最近学了下python,打算写个批量下载图片的。单线程搞定了,多线程就不行了(网上找的多线程代码)。因为我发现单线程和多线程的下载耗时差不多。。哪位大佬帮忙琢磨下。谢谢
#coding=utf-8
import requests
import re
import json
import os
import time
import threading
#自定义函数啦
#漫画下载函数
def mh_down(url):
r = requests.get(url)
r.encoding = 'UTF-8'
relink = 'chapterImages = (.*);var chapterPath'
cinfo = re.findall(relink,r.text)
mh_json = json.loads(cinfo)
#获取图片地址中间部分
relink1 = 'var chapterPath = "(.*)";var chapterPrice ='
cinfo1 = re.findall(relink1,r.text)
mh_zj = cinfo1
#单线程下载图片
'''
start = time.time()
for key in mh_json:
#https://img001.yayxcc.com/images/comic/111/221325/1526971083Mw1MsFzdRMx9Pedi.jpg
mh_pic = 'https://img001.yayxcc.com/%s%s'%(mh_zj,key)
response = requests.get(mh_pic)
with open(os.path.join(r'C:\py\mh', key), 'wb') as f:
f.write(response.content)
end = time.time()
return 'Running time: %s Seconds'%(end-start)
'''
#多线程下载图片
start = time.time()
thread_list = []
threads_max_count = 100 # 线程总数
count = 0
num = 0
for item in mh_json:
if count < threads_max_count: #当前线程数小于线程总数时
mh_pic = 'https://img001.yayxcc.com/%s%s'%(mh_zj,item)
th = threading.Thread(target=mh_down_dxc(mh_pic,item))
th.start() # 启动线程下载图片
thread_list.append(th)
count += 1 # 线程计数器+1
num += 1
elif count == threads_max_count: # 当当前线程数量 等于 最大值
judge_alive_count = 0 # 初始化判断存活的线程的数目
for i in range(num):
if thread_list.isAlive(): # 如果线程仍存活,计数器+1
judge_alive_count += 1
if count > judge_alive_count: # 如果线程计数值大于判断存活的计数值
count = judge_alive_count # 将当前的线程数值置为存活数量
end = time.time()
return 'Running time: %s Seconds'%(end-start)
#下载图片啦
def mh_down_dxc(url,key):
response = requests.get(url)
with open(os.path.join(r'C:\py\mh', key), 'wb') as f:
f.write(response.content)
print(mh_down('https://www.36mh.com/manhua/zhanlansedeshijie/327505.html'))
页:
[1]