找代码的咸鱼 2021-10-03 22:06 采纳率: 100%
浏览 276
已结题

一个关于Event loop is closed的问题

代码如下:

import asyncio
import aiohttp
from lxml import etree
import requests
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
#/html/body/div[13]/div[1]/div[2]/dl/dd/a/img
async def download(url):
    name = url.rsplit('/',1)[1]
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as res:
            with open(f'彼岸桌面壁纸下载/{name}','wb') as f:
                f.write(await res.content.read())
async def main():
    tasks=[]
    for url in urls:
        tasks.append(download(url))
    await asyncio.wait(tasks)
if __name__ == '__main__':
    url0 = 'https://www.enterdesk.com/zhuomianbizhi/'
    text = requests.get(url0).text
    tree = etree.HTML(text)
    urls = tree.xpath('/html/body/div[13]/div[1]/div/dl/dd/a/img/@src')
    asyncio.run(main())

报错如下:
D:\MyPcharmProject\venv\Scripts\python.exe D:/MyPcharmProject/爬虫/Umei.py
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000023AD6F1C670>
Traceback (most recent call last):
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 711, in call_soon
self._check_closed()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 504, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Process finished with exit code 0

  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 创建了问题 10月3日

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启