鱼C论坛

 找回密码
 立即注册
查看: 1754|回复: 3

运行一段时间显示Eventloop is closed

[复制链接]
发表于 2021-10-1 21:52:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下:(这里提前创建了‘彼岸桌面壁纸下载’这个文件夹用来放图片)
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())

if __name__ == '__main__':
    url0 = f'https://www.enterdesk.com/zhuomianbizhi/{0}.html'
    urls = []
    for i in range(10):
        text = requests.get(url0.format(i)).text
        tree = etree.HTML(text)
        url = tree.xpath('/html/body/div[13]/div[1]/div/dl/dd/a/img/@src')
        urls.extend(url)
    print(urls)
    loop = asyncio.get_event_loop()
    tasks = [loop.create_task(download(url)) for url in urls]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()

报错如下:(我有尝试去掉loop.close,这次没报错,但是爬取图片数量和报错的一样多)
D:\MyPcharmProject\venv\Scripts\python.exe D:/MyPcharmProject/爬虫/Umei.py
['https://up.enterdesk.com/edpic_360_360/7c/b3/21/7cb32180d0c74bd6f9a3ec6e32a8474b.jpg', 'https://up.enterdesk.com/edpic_360_360/0f/e3/73/0fe373591a738375bb25a949cc993d17.jpg', 'https://up.enterdesk.com/edpic_360_360/ed/64/b3/ed64b3faff775c57a3fe7c3000c4f61f.jpg', 'https://up.enterdesk.com/edpic_360_360/1b/e1/7f/1be17fbc8443df8b46d91b420be75e69.jpg'
................................................................................
................................................................................(都是和上下文一样的图片网址)
'https://up.enterdesk.com/edpic_360_360/d0/48/ac/d048ac0930025d5bda22a8c80f21db68.jpg', 'https://up.enterdesk.com/edpic_360_360/f7/f1/25/f7f12509b55471d79718ea80e2678c49.jpg', 'https://up.enterdesk.com/edpic_360_360/38/52/91/385291c55d44b6c47edfa52767e6f687.jpg', 'https://up.enterdesk.com/edpic_360_360/89/2a/1f/892a1fdb1e0b3433b08ee9312c6b8f57.jpg']
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001DF9942B5E0>
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
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001DF9942B5E0>
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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-2 15:38:10 | 显示全部楼层
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():
    url0 = f'https://www.enterdesk.com/zhuomianbizhi/{0}.html'
    urls = []
    for i in range(10):
        text = requests.get(url0.format(i)).text
        tree = etree.HTML(text)
        url = tree.xpath('/html/body/div[13]/div[1]/div/dl/dd/a/img/@src')
        urls.extend(url)
    print(urls)

    tasks = [asyncio.create_task(download(url)) for url in urls]
    await asyncio.wait(tasks)

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-3 17:52:18 | 显示全部楼层

不行呀,虽然没报错,但图片就下载了几张
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-9 00:52:57 | 显示全部楼层
一只魈咸鱼 发表于 2021-10-3 17:52
不行呀,虽然没报错,但图片就下载了几张
import asyncio
import aiohttp
from lxml import etree
import requests
from bs4 import BeautifulSoup

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())


def get_pic_url(url):
    soup = BeautifulSoup(requests.get(url).text, 'lxml')
    egeli_pic_li_list = soup.find_all('div', class_='egeli_pic_li')
    return  [
        egeli_pic_li.find('dl', class_='egeli_pic_dl').find('dd').find('a').find('img').get('src')
        for egeli_pic_li in egeli_pic_li_list
        if egeli_pic_li.find('dl', class_='egeli_pic_dl')
    ]

async def main():
    # url0 = f'https://www.enterdesk.com/zhuomianbizhi/{0}.html'
    urls = []
    # for i in range(10):
    #     text = requests.get(url0.format(i)).text
    #     tree = etree.HTML(text)
    #     url = tree.xpath('/html/body/div[13]/div[1]/div/dl/dd/a/img/@src')
    #     urls.extend(url)
    for i in range(1, 10):
        url = f'https://www.enterdesk.com/zhuomianbizhi/{i}.html'
        urls.extend(get_pic_url(url))

    tasks = [asyncio.create_task(download(url)) for url in urls]
    await asyncio.wait(tasks)


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())
问题在lxml这个库构造的url, 协程是正常的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-13 03:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表