代码:
1 import urllib.request 2 import gevent 3 from gevent import monkey 4 5 6 monkey.patch_all() 7 8 9 def downloader(img_name,img_url): 10 req = urllib.request.urlopen(img_url) 11 img_content = req.read() 12 with open(img_name,"wb") as f: 13 f.write(img_content) 14 15 16 17 def main(): 18 gevent.joinall([ 19 # gevent.spawn(函数名,"要储存的文件名","图片网址") 20 gevent.spawn(downloader,"2.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566476963330&di=50d0898e8dd5cdcd5b898d79726cf3c7&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fb4870f80a9accb1cce5eaf8e186fe9938e2091d17d4ce-qoXBpf_fw658"), 21 gevent.spawn(downloader,"3.jpg","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566476963329&di=c543415f9006fd8a14121fd40da2bde5&imgtype=0&src=http%3A%2F%2Fimg1.ph.126.net%2Fv3aon1cLTdZw0xUYgtmxMA%3D%3D%2F6597693189749236541.jpg") 22 ]) 23 24 25 if __name__ == '__main__': 26 main()