import asyncio
import aiohttp
urls = [
"http://kr.shanghai-jiuxin.com/file/mmvv/20221009/ipoumjyaktx.jpg",
"http://kr.shanghai-jiuxin.com/file/mmvv/20221009/da55keescxi.jpg",
"http://kr.shanghai-jiuxin.com/file/mmvv/20221009/usp2go55ec1.jpg"
]
async def aiodownload(url):
# aiohttp.ClientSession() == requests
# 发送请求
# 得到图片
# 保存文件
name = url.rsplit("/", 1)[1]
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp: # resp.content.read() == resp.content # resp.text() == resp.text
with open(name, mode="wb") as f:
f.write(await resp.content.read()) # 读取内容是异步的 需要挂起
print(name, "搞定")
async def main():
tasks = []
for url in urls:
tasks.append(aiodownload(url))
await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.run(main())
异步http请求爬虫模板,随便写的 看模板就行