找回密码
 立即注册
CeraNetworksBGVM服务器主机交流IP归属甄别会员请立即修改密码
查看: 630|回复: 1

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

[复制链接]

7

主题

24

回帖

137

积分

注册会员

积分
137
发表于 2019-1-6 23:32:56 | 显示全部楼层 |阅读模式
本帖最后由 mingtian 于 2019-1-6 23:34 编辑

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

[ol]
  • #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[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]复制代码
  • 本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×
    回复

    使用道具 举报

    6

    主题

    31

    回帖

    94

    积分

    注册会员

    积分
    94
    发表于 2019-1-7 12:52:50 | 显示全部楼层
    不应该是同时多个任务,而不是一个任务多个线程吗?
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|HS2V主机综合交流论坛

    GMT+8, 2024-4-16 21:59 , Processed in 0.080017 second(s), 4 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2023 Discuz! Team.

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