多任务爬虫-协程

from urllib.request import *
import gevent
import time
from gevent import monkey

# 打补丁
monkey.patch_all()


def download(save_path, url):
    """
    下载图片
    :param save_path: 保存路径
    :param url: 图片地址
    :return: none
    """
    print('开始')
    # 打开
    request_url = urlopen(url)
    # 读取
    content = request_url.read()
    # 写入
    with open(save_path, "wb") as f:
        f.write(content)
    time.sleep(2)
    print('结束')


# 打开
def main():
    # 图片连接地址列表
    url_list = list()
    url1='http://i1.iblue.com/cztv/cms/2018/05/21/78371a5e43c44c129f6d7d0608d34dea3617289287808318700.jpg'
    url2='http://i1.iblue.com/cztv/cms/2018/04/16/1ba62af773a542dbb6264525708de1b77159447140013465180.jpg'
    url3='http://i0.letvimg.com/lc05_isvrs/201707/14/20/29/6cf30fc48dab4af4905c3b64239b16c83579014454080268404.jpg'
    url_list.append(url1)
    url_list.append(url2)
    url_list.append(url3)

    # 初始化保存路径
    n = 0

    # 等待空列表
    spawn_list = list()

    for url in url_list:
        # 保存路径
        save_path = "%s.jpg" % n

        # download(save_path, url)
        # 任务加入到协程
        spawn_download = gevent.spawn(download,save_path, url)
        # 任务加入到等待列表
        spawn_list.append(spawn_download)

        n += 1
    # 主线程等待
    gevent.joinall(spawn_list)

if __name__ == '__main__': 
    main()

 

你可能感兴趣的:(python基础)