У меня есть простой API фляги с одной конечной точкой, который вызывает метод в другом файле для рендеринга некоторого javascript с сайта, используя request-html
@app.route('/renderJavascript')
def get_attributes():
return get_item_attributes('https://www.site.com.mx/site.html')
Код метода выглядит следующим образом:
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.
После вызова конечной точки я получаю эту ошибку:
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 handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python37\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Python37\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python37\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "API.py", line 35, in get_attributes
return get_item_attributes('https://www.shein.com.mx/Floral-Print-Raglan-Sleeve-Curved-Hem-Tee-p-858258-cat-1738.html')
File "C:\Users\xChapx\Desktop\Deving\API\request.py", line 25, in get_item_attributes
resp.html.render()
File "C:\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:\Python37\lib\site-packages\requests_html.py", line 727, in browser
self.loop = asyncio.get_event_loop()
File "C:\Python37\lib\asyncio\events.py", line 644, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-1'.
Я прочитал в Интернете, что HTMLSession работает неправильно, если он используется вне основного потока, так как фляга работает в потокевозможно, именно это и является причиной ошибки.