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.02405.com?n={i}" for i in range(1, 10)]
tasks = [download(url) for url in urls]
task = asyncio.wait(tasks)
asyncio.run(task)
零五网 - 为热爱技术的人而生!
Python aiohttp 异步爬虫简单示例代码
0 Previous ArticlePython基于paramiko包实现SSH连接服务器
Next Article Python 使用 openpyxl 操作 Excel