爬虫协程爬取图片

import aiohttp
import asyncio

urls = [
    "https://images.quanjing.com/170/thu/pc041-04.jpg",
    "https://images.quanjing.com/Chineseview005/thu/1S-1095.jpg",
    "https://images.quanjing.com/chineseview019/thu/27-0270.jpg"
]

async def adownload(url):
    name = url.rsplit("/", 1)[1]
    async with aiohttp.ClientSession as session:
        async with session.get(url) as resp:
            with open(name, mode="wb") as f:
                f.write(await resp.content.read())
    print(name, "over!!!")

async def main():
    tasks = []
    for url in urls:
        tasks.append(adownload(url))

if __name__ == "__main__":
    asyncio.run(main())

上一篇:使用 tensorboard 可视化模型、数据和训练


下一篇:OPENCV颜色检测——库函数版本