Python aiohttp 异步爬虫简单示例代码

admin 阅读:68 2024-03-01
import asyncio
import aiohttp

async def fetch(session, url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
    }
    async with session.get(url, headers=headers) as resp:
        return await resp.text()


async def download(url):
    async with aiohttp.ClientSession() as session:
        resp = await fetch(session, url)
        print(resp)


urls = [f"https://www.codesou.cn?n={i}" for i in range(1, 10)]
tasks = [download(url) for url in urls]
task = asyncio.wait(tasks)
asyncio.run(task)
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!