xumb

一、多线程下载文件

 1 import requests,time,threading
 2 from hashlib import md5
 3 result = {}
 4 def down_load_pic(url):
 5     req = requests.get(url)
 6     m = md5(url.encode())
 7     with open( m.hexdigest()+\'.png\',\'wb\') as fw:
 8         fw.write(req.content)
 9 
10 url_list = [\'http://www.nnzhp.cn/wp-content/uploads/2019/10/f410afea8b23fa401505a1449a41a133.png\',
11             \'http://www.nnzhp.cn/wp-content/uploads/2019/11/481b5135e75c764b32b224c5650a8df5.png\',
12             \'http://www.nnzhp.cn/wp-content/uploads/2019/11/b23755cdea210cfec903333c5cce6895.png\',
13             \'http://www.nnzhp.cn/wp-content/uploads/2019/11/542824dde1dbd29ec61ad5ea867ef245.png\']
14 
15 #单线程下载
16 # start_time = time.time()
17 # for url in url_list:
18 #     down_load_pic(url)
19 # end_time = time.time()
20 # print(end_time - start_time)
21 
22 #多线程下载
23 start_time = time.time()
24 for url in url_list:
25     t = threading.Thread(target=down_load_pic,args=(url,))
26     t.start()
27 while threading.activeCount() !=1:
28     pass
29 end_time = time.time()
30 print(end_time - start_time)

 

分类:

技术点:

相关文章:

  • 2021-06-04
  • 2022-12-23
  • 2021-11-18
  • 2021-09-25
  • 2022-01-13
  • 2021-12-10
  • 2022-12-23
猜你喜欢
  • 2021-08-20
  • 2021-06-20
  • 2022-12-23
相关资源
相似解决方案