RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0' in python

RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0' in python

我正在尝试 运行 在 Google Cloud Function 中的 python 脚本下面。

运行时间:Python3.8

入口点:hello_world

from requests_html import HTMLSession
def hello_world(request):
    session = HTMLSession()
        
    r = session.get('https://translate.google.com')
    r.html.render()  
    app = r.html.find('#yDmH0d')
    for value in app:
        return value.text[:10]

触发事件:{}

当我在 google 云平台上测试这个功能时,出现以下错误。

Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, 
    in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, 
    in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, 
    in handle_user_exception reraise(exc_type, exc_value, tb) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, 
    in reraise raise value File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, 
    in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, 
    in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 87, 
    in view_func return function(request._get_current_object()) File "/workspace/main.py", line 6, 
    in hello_world r.html.render() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 586, 
    in render self.browser = self.session.browser # Automatically create a event loop and browser File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 727, 
    in browser self.loop = asyncio.get_event_loop() File "/opt/python3.8/lib/python3.8/asyncio/events.py", line 639, 
    in get_event_loop raise RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.
Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, 
    in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, 
    in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, 
    in handle_user_exception reraise(exc_type, exc_value, tb) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, 
    in reraise raise value File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, 
    in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, 
    in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 87, 
    in view_func return function(request._get_current_object()) File "/workspace/main.py", line 6, 
    in hello_world r.html.render() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 586, 
    in render self.browser = self.session.browser # Automatically create a event loop and browser File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests_html.py", line 727,
    in browser self.loop = asyncio.get_event_loop() File "/opt/python3.8/lib/python3.8/asyncio/events.py", line 639, 
    in get_event_loop raise RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.

如何解决这个错误?

Request-HTML 网站上它说只支持 Python 3.6,因此无法在 Cloud Functions 上 运行 它。

在这种情况下,您可能想尝试另一个网络抓取工具。