无法对url发出异步请求,无法从错误中获取响应。在
File "D:\Dev\Scripts\ol_as.py", line 28, in main async with requests_html.AsyncHTMLSession() as session:
AttributeError: aexit
import asyncio
import requests_html
from time import time
from bs4 import BeautifulSoup
async def fetch_content(url, session):
async with session.get(url, allow_redirects=True) as response:
data = await respone.read()
respone.html.render()
soup = BeautifulSoup(respone.html.html, 'lxml')
txt = soup.find_all('span', {'class': 'text'})
print(txt)
async def main():
url = 'http://quotes.toscrape.com/js/'
tasks = []
async with requests_html.AsyncHTMLSession() as session:
for i in range(10):
tasks.append(asyncio.create_task(fetch_content(url, session)))
await asyncio.gather(*tasks)
if __name__ == '__main__':
t0 = time()
asyncio.run(main())
print(time() - t0)
你已经很接近了。从AsyncHTMLSession的实验来看,它不喜欢在上下文管理器中使用并传递给不同的协同程序。你也需要r。html.arender而不仅仅是渲染。在
如果你想要一份指定页数的引文列表,我想到的是:
相关问题 更多 >
编程相关推荐