马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import asyncio
import aiohttp
urls = ["https://scpic.chinaz.net/files/pic/pic9/202108/apic34745.jpg",
"https://scpic.chinaz.net/files/pic/pic9/202106/apic33089.jpg",
"https://scpic.chinaz.net/files/pic/pic9/202105/apic32801.jpg",
"https://scpic.chinaz.net/files/pic/pic9/202105/hpic3919.jpg"]
async def aiodownload(url):
name = url.rsplit("/", 1)[1]
# print(name)
async with aiohttp.ClientSession() as session: # 这里相当于requests
async with session.get(url) as resp: # resp = requests.get()
# 请求回来后写入文件
with open(name, 'wb') as f: # 创建文件
f.write(await resp.content.read()) # 读取内容是异步的,需要await挂起
print(name, "下载完成!")
async def main():
task = []
for url in urls:
task.append(asyncio.create_task(aiodownload(url)))
await asyncio.wait(task)
if __name__ == '__main__':
asyncio.run(main())
这是一个异步协程的小程序,爬取几张美女的图片,
爬取是成功的,但是同时也报错了。
RuntimeError: Event loop is closed
这是什么原因啊?
哪位前辈帮忙看看,能否修改一下,谢谢啊 |