1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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.baidu.com?n={i}" for i in range(1, 10)] tasks = [download(url) for url in urls] task = asyncio.wait(tasks) asyncio.run(task)
|