简单示例代码:
# 描述:爬取网页中的图片,
# 此处就给一些图片的连接,对图片下载。
# 采用并发的方式
# 此处给好的图片链接分别为:
import urllib.request
import gevent
from gevent import monkey
monkey.patch_all()
# def main():
# req = urllib.request.urlopen("https://rpic.douyucdn.cn/live-cover/roomCover/cover_update/2019/09/20/11aa0f39ee5bd02e402cc90394e9bde4.jpg")
# img_content = req.read()
# with open('1.jpg', 'wb') as f:
# f.write(img_content)
# 下载图片
def downpic(url, filename):
req = urllib.request.urlopen(url)
img_content = req.read()
with open(filename, 'wb') as f:
f. write(img_content)
# 用协程的方式。
# gevent
gevent.joinall([
gevent.spawn(downpic, "https://rpic.douyucdn.cn/live-cover/roomCover/cover_update/2019/09/20/xxxcc90394e9bde4.jpg", '1.jpg'),
gevent.spawn(downpic, "https://rpic.douyucdn.cn/live-cover/roomCover/cover_update/2019/09/06/xx2f16522f1dc7cf.jpg", '2.jpg'),
gevent.spawn(downpic, "https://rpic.douyucdn.cn/asrpic/190924/161xx9.png", '3.png')
])