Python请求\u html抛出错误线程“ThreadPoolExecutor0\u 0”中没有当前事件循环

2024-09-22 22:28:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用python请求\u html模块为给定url运行JS script,但几乎没有收到错误

能帮我个忙吗

代码:-

from requests_html import HTMLSession
def rrr(request):
    session = HTMLSession()
    url = 'https://careers.microsoft.com/'
    response = session.get(url)
    script = """
                () => {
                    return {
                        ans: window.location.href,
                    }
                }
            """
    ans = response.html.render(script=script)
    return JsonResponse(ans, safe=False)

错误:-

  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/home/mahesh/Documents/dojo/dojo/dojo/tenants/api.py", line 242, in rrr
    ans = response.html.render(script=script)
  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/requests_html.py", line 586, in render
    self.browser = self.session.browser  # Automatically create a event loop and browser
  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/requests_html.py", line 727, in browser
    self.loop = asyncio.get_event_loop()
  File "/home/mahesh/anaconda3/lib/python3.7/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'.

在终端中运行时,它工作正常,如下所示

enter image description here


Tags: inpyloophomeresponselibpackageshtml