1. 代码
import gevent
import urllib.request
from gevent import monkey
monkey.patch_all()
def download(image_name, image_url):
req = urllib.request.urlopen(image_url)
image_content = req.read()
with open(image_name, "wb") as f:
f.write(image_content)
def main():
image1_url = "https://i2.hdslb.com/bfs/archive/eff211eb5b5834ebf537c6c8e6aff8434be642e4.jpg@412w_232h_1c.jpg"
image2_url = "https://i2.hdslb.com/bfs/archive/46dfbee6905aebdd197b1a9d06902746ce6d7ad6.jpg@412w_232h_1c.jpg"
image3_url = "https://i0.hdslb.com/bfs/archive/6e90f6b996da5c9277935e45b4da7f11add384f5.jpg@412w_232h_1c"
gevent.joinall([
gevent.spawn(download, "1.jpg", image1_url),
gevent.spawn(download, "2.jpg", image2_url),
gevent.spawn(download, "3.jpg", image3_url),
])
if __name__ == "__main__":
main()