|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
|
|