DEV Community


Python requests_html is throwing error There is no current event loop in thread 'ThreadPoolExecutor-0_0'

Sai Avinash Duddupudi
Software Engineer
・1 min read

I am trying to run a JS script using python requests_html module for a given url but was getting few errors.

Can some please help me out where I am doing wrong?

Code :-

from requests_html import HTMLSession
def rrr(request):
    session = HTMLSession()
    url = ''
    response = session.get(url)
    script = """
                () => {
                    return {
                        ans: window.location.href,
    ans = response.html.render(script=script)
    return JsonResponse(ans, safe=False)
Enter fullscreen mode Exit fullscreen mode

Error :-

  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/rest_framework/", line 476, in raise_uncaught_exception
    raise exc
  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/rest_framework/", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/rest_framework/", line 50, in handler
    return func(*args, **kwargs)
  File "/home/mahesh/Documents/dojo/dojo/dojo/tenants/", line 242, in rrr
    ans = response.html.render(script=script)
  File "/home/mahesh/anaconda3/lib/python3.7/site-packages/", 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/", line 727, in browser
    self.loop = asyncio.get_event_loop()
  File "/home/mahesh/anaconda3/lib/python3.7/asyncio/", line 644, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.
Enter fullscreen mode Exit fullscreen mode

When ran in terminal, it worked fine as follows

enter image description here

Discussion (0)