全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 2981|回复: 2

python 多线程批量下载图片问题

[复制链接]
发表于 2019-1-6 23:32:56 | 显示全部楼层 |阅读模式
本帖最后由 mingtian 于 2019-1-6 23:34 编辑

最近学了下python,打算写个批量下载图片的。单线程搞定了,多线程就不行了(网上找的多线程代码)。因为我发现单线程和多线程的下载耗时差不多。。哪位大佬帮忙琢磨下。谢谢

  1. #coding=utf-8

  2. import requests
  3. import re
  4. import json
  5. import os
  6. import time
  7. import threading



  8. #自定义函数啦
  9. #漫画下载函数
  10. def mh_down(url):
  11.         r = requests.get(url)
  12.         r.encoding = 'UTF-8'
  13.         relink = 'chapterImages = (.*);var chapterPath'
  14.         cinfo = re.findall(relink,r.text)
  15.         mh_json = json.loads(cinfo[0])
  16.         #获取图片地址中间部分
  17.         relink1 = 'var chapterPath = "(.*)";var chapterPrice ='
  18.         cinfo1 = re.findall(relink1,r.text)
  19.         mh_zj = cinfo1[0]
  20.         #单线程下载图片
  21.         '''
  22.         start = time.time()
  23.         for key in mh_json:
  24.                 #https://img001.yayxcc.com/images/comic/111/221325/1526971083Mw1MsFzdRMx9Pedi.jpg
  25.                 mh_pic = 'https://img001.yayxcc.com/%s%s'%(mh_zj,key)
  26.                 response = requests.get(mh_pic)
  27.                 with open(os.path.join(r'C:\py\mh', key), 'wb') as f:
  28.                         f.write(response.content)
  29.         end = time.time()
  30.         return 'Running time: %s Seconds'%(end-start)
  31.         '''
  32.         #多线程下载图片
  33.         start = time.time()
  34.         thread_list = []
  35.         threads_max_count = 100 # 线程总数
  36.         count = 0
  37.         num = 0
  38.         for item in mh_json:
  39.                 if count < threads_max_count: #当前线程数小于线程总数时
  40.                         mh_pic = 'https://img001.yayxcc.com/%s%s'%(mh_zj,item)
  41.                         th = threading.Thread(target=mh_down_dxc(mh_pic,item))
  42.                         th.start() # 启动线程下载图片
  43.                         thread_list.append(th)
  44.                         count += 1 # 线程计数器+1
  45.                         num += 1
  46.                 elif count == threads_max_count: # 当当前线程数量 等于 最大值
  47.                         judge_alive_count = 0 # 初始化判断存活的线程的数目
  48.                         for i in range(num):
  49.                                 if thread_list[i].isAlive(): # 如果线程仍存活,计数器+1
  50.                                         judge_alive_count += 1
  51.                                 if count > judge_alive_count: # 如果线程计数值大于判断存活的计数值
  52.                                         count = judge_alive_count # 将当前的线程数值置为存活数量
  53.         end = time.time()
  54.         return 'Running time: %s Seconds'%(end-start)
  55. #下载图片啦
  56. def mh_down_dxc(url,key):
  57.         response = requests.get(url)
  58.         with open(os.path.join(r'C:\py\mh', key), 'wb') as f:
  59.                 f.write(response.content)
  60. print(mh_down('https://www.36mh.com/manhua/zhanlansedeshijie/327505.html'))
复制代码
发表于 2019-1-7 12:52:50 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2019-1-31 20:54:03 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-3-28 22:37 , Processed in 0.057798 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表