Python async异步编程爬取图片

Python async异步编程爬取图片

import aiohttp
import asyncio
import time

async def spider_img(url):

    async with aiohttp.ClientSession() as session:

        response=await session.get(headers=headers, url=url)

        content_img = await response.read()

        return content_img

img_lit=['https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2615992304.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2615015805.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2614997281.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2614950073.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2614997041.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2614691271.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2615284298.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2614594787.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2614949805.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2614988097.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2614594570.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2614385032.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2605928267.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2615497491.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2555295759.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p1599059779.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2614500706.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2455050536.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2614500649.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2614204949.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2614359276.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2561721372.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2264377763.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2361744534.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2614954783.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2316177058.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2257944916.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2615017094.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2615418974.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2616349563.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2616742356.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2572231147.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2616436093.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2615604324.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2576628914.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2613145166.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2616349755.jpg']

def async_main():
    loop = asyncio.get_event_loop()
    tasks = [spider_img(url) for url in img_lit]
    loop.run_until_complete(asyncio.wait(tasks))

if __name__ == '__main__':
    start = time.time()
    async_main()
    print('耗时:%d秒'%(time.time() - start))

打印结果:
耗时:3秒

你可能感兴趣的:(异步,Python,python)