我在爬虫中使用了协程,但是运行起来还是同步操作,没有达到异步效果,请问是哪里出了问题了,还请指正一下代码
不胜感激
import requests
from lxml import etree
import aiohttp
import asyncio
import aiofiles
import time
# 提取第二页URL
async def index_url(number):
img_urls = []
async with aiohttp.ClientSession() as session:
async with await session.get(f'https://wallpaperscraft.com/catalog/anime/page{number}') as resp:
tree = etree.HTML(await resp.text()).xpath('/html/body/div/div[2]/div[2]/div/div[2]/div[1]/ul/li//a/@href')
for t in tree:
url = 'https://wallpaperscraft.com' + t
img_urls.append(url)
return img_urls
# 请求第二页URL
async def to_imgurl(url):
async with aiohttp.ClientSession() as session:
async with await session.get(url) as resp:
src = etree.HTML(await resp.text()).xpath('/html/body/div/div[2]/div[2]/div/div[2]/div[1]/div[1]/img/@src')
print(src)
if __name__ == '__main__':
for i in range(1, 4):
list_url = asyncio.run(index_url(i))
for l in list_url:
asyncio.run(to_imgurl(l))