运行一段时间显示Eventloop 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/div/div/dl/dd/a/img
async def download(url):
name = url.rsplit('/',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/div/div/dl/dd/a/img/@src')
urls.extend(url)
print(urls)
loop = asyncio.get_event_loop()
tasks =
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
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/div/div/dl/dd/a/img
async def download(url):
name = url.rsplit('/',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/div/div/dl/dd/a/img/@src')
urls.extend(url)
print(urls)
tasks =
await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main()) qaoapp 发表于 2021-10-2 15:38
不行呀,虽然没报错,但图片就下载了几张 一只魈咸鱼 发表于 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/div/div/dl/dd/a/img
async def download(url):
name = url.rsplit('/',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/div/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 =
await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main()) 问题在lxml这个库构造的url, 协程是正常的
页:
[1]