python3 协程简单运用爬取两张妹子图

废话不多书,直接上代码:

import ssl
import urllib.request
import gevent
from gevent import monkey

monkey.patch_all(select=False)

ssl._create_default_https_context = ssl._create_unverified_context
def downloader(img_name, img_url):
    req = urllib.request.urlopen(img_url)
    img_content = req.read()

    with open(img_name, "wb") as f:
        f.write(img_content)

def main():
    gevent.joinall([
            gevent.spawn(downloader, "美女.jpg", "https://rpic.douyucdn.cn/appCovers/2017/09/22/1760931_20170922133718_big.jpg"),
            gevent.spawn(downloader, "漂亮.jpg", "https://rpic.douyucdn.cn/appCovers/2017/09/17/2308890_20170917232900_big.jpg")
    ])


if __name__ == '__main__':
    main()

运行成功的标志:

无报错,即可。

效果展示:

欢迎交流!

posted @ 2019-07-22 20:47  青春叛逆者  阅读(171)  评论(0编辑  收藏  举报