【发布时间】:2020-12-24 07:39:36
【问题描述】:
我正在尝试使用 requests_html 库和 js 渲染以及 FastAPI:
脚本.py
from fastapi import FastAPI
from requests_html import HTMLSession
app = FastAPI()
@app.get('/')
def func():
with HTMLSession() as session:
r = session.get('https://stackoverflow.com')
r.html.render()
return r.text
使用uvicorn script:app --reload 运行并访问http://127.0.0.1:8000/ 时
,我收到以下错误:
...
r.html.render()
File "c:\users\a\appdata\local\programs\python\python37\lib\site-packages\requests_html.py", line 586, in render
self.browser = self.session.browser # Automatically create a event loop and browser
File "c:\users\a\appdata\local\programs\python\python37\lib\site-packages\requests_html.py", line 727, in browser
self.loop = asyncio.get_event_loop()
File "c:\users\a\appdata\local\programs\python\python37\lib\asyncio\events.py", line 644, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.
知道如何让它们一起工作吗?
【问题讨论】:
标签: python python-3.x api fastapi python-requests-html