小编xCh*_*apx的帖子

requests-html“运行时错误:在烧瓶端点上使用线程“Thread-1”时,线程“Thread-1”中没有当前事件循环

我有一个简单的 Flask API,其中一个端点调用另一个文件中的方法,以使用 request-html 从站点呈现一些 javascript

@app.route('/renderJavascript')
def get_attributes():
    return get_item_attributes('https://www.site.com.mx/site.html')
Run Code Online (Sandbox Code Playgroud)

该方法的代码如下所示:

from requests_html import HTMLSession
from bs4 import BeautifulSoup

def get_item_attributes(url):
    #Connecting to site.
    session = HTMLSession()
    resp = session.get(url)
    resp.html.render()
    resp.session.close()
    soup = BeautifulSoup(resp.html.html,'lxml')

    ................................
    #Rest of the code is handling the data with bs4 and returning a json.
Run Code Online (Sandbox Code Playgroud)

调用端点后,我收到此错误:

Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python37\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python37\lib\site-packages\flask\app.py", line 1820, in …
Run Code Online (Sandbox Code Playgroud)

python multithreading event-loop flask python-requests-html

6
推荐指数
1
解决办法
1392
查看次数