|
本帖最后由 mingtian 于 2019-1-6 23:34 编辑
最近学了下python,打算写个批量下载图片的。单线程搞定了,多线程就不行了(网上找的多线程代码)。因为我发现单线程和多线程的下载耗时差不多。。哪位大佬帮忙琢磨下。谢谢
[ol]#coding=utf-8 import requestsimport reimport jsonimport osimport timeimport 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[0]) #获取图片地址中间部分 relink1 = 'var chapterPath = "(.*)";var chapterPrice =' cinfo1 = re.findall(relink1,r.text) mh_zj = cinfo1[0] #单线程下载图片 ''' start = time.time() for key in mh_json: #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 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'))[/ol]复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|