67 – 用多线程从网上下载图像文件

在urls.txt 文件中包含了若干个图像url,一行一个url,请使用多线程下载这些图像文件,并按url出现的顺序保存为 0.jpg、1.jpg、2.jpg,以此类推

urls.txt

https://photo.16pic.com/00/26/41/16pic_2641402_b.jpg
https://photo.16pic.com/00/21/52/16pic_2152258_b.jpg
https://photo.16pic.com/00/44/47/16pic_4447412_b.jpg
https://photo.16pic.com/00/20/47/16pic_2047443_b.jpg

from urllib3 import *
import threading
import os

http = PoolManager()

disable_warnings()

f = open('./files/urls.txt', 'r')
urlList = []
while True:
    url = f.readline()
    if url == '':
        break
    urlList.append(url.strip())
f.close()

# print(urlList)

class DownloadThread(threading.Thread):
    def __init__(self, func, args):
        super().__init__(target=func, args=args)
        
def download(filename, url):
    response = http.request('GET', url)
    f = open(filename, 'wb')
    f.write(response.data)
    f.close
    print('<', url, '>', '下载完成')
    
root = './files/img'
if not os.path.exists(root):
    os.makedirs(root)
    
for i in range(len(urlList)):
    filename = root + '/' + str(i) + '.jpg'
    thread = DownloadThread(download, (filename, urlList[i]))
    thread.start()

< https://photo.16pic.com/00/20/47/16pic_2047443_b.jpg > 下载完成

< https://photo.16pic.com/00/26/41/16pic_2641402_b.jpg > 下载完成

< https://photo.16pic.com/00/21/52/16pic_2152258_b.jpg > 下载完成

< https://photo.16pic.com/00/44/47/16pic_4447412_b.jpg > 下载完成

正文完