问题描述
我正在尝试在 Google Cloud Function 中的 python 脚本下运行。
运行时:Python 3.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]
触发事件:{}
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",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 上运行它。
在这种情况下,您可能想尝试另一个网络抓取工具。