Why is the error "Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead"?
Asked Answered
S

4

11

I'm running the code provided by @Dan-Dev in his answer.

from requests_html import HTMLSession

url = 'https://www.thefreedictionary.com/love'
session = HTMLSession()
r = session.get(url)
r.html.render()
lang_bar = r.html.find('#LangBar', first=True)
print(lang_bar.html)

and the result is

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-ec1d9137b197> in <module>
      8 
      9 resp = session.get(url, headers = headers)
---> 10 resp.html.render()
     11 
     12 soup = bs(resp.html.html, "lxml")

C:\Anaconda3\lib\site-packages\requests_html.py in render(self, retries, script, wait, scrolldown, sleep, reload, timeout, keep_page)
    584         """
    585 
--> 586         self.browser = self.session.browser  # Automatically create a event loop and browser
    587         content = None
    588 

C:\Anaconda3\lib\site-packages\requests_html.py in browser(self)
    727             self.loop = asyncio.get_event_loop()
    728             if self.loop.is_running():
--> 729                 raise RuntimeError("Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.")
    730             self._browser = self.loop.run_until_complete(super().browser)
    731         return self._browser

RuntimeError: Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.

Clearly, the code runs fine in Dan-Dev's computer. Could you please explain why my laptop returns an error Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead?

Statistical answered 23/8, 2020 at 8:49 Comment(1)
Can you post details of your environment OS Python version etc? Are you using Jupyter Notebook? Have you tried using AsyncHTMLSession instead of HTMLSession?Incompletion
N
5

You may try this:

import nest_asyncio

nest_asyncio.apply()

session = HTMLSession()
r = session.get("URL")

html_str = r.text
Nickels answered 23/9, 2020 at 7:20 Comment(2)
Where does url_template come from? No import in your original answer.Dunne
I was usiing a url template to build url dynamically. This is not important here so I have just replaced this by a random url.Nickels
B
5

Use arender() instead of render() or set the script as async

Borage answered 1/10, 2022 at 17:47 Comment(0)
P
2

You can save these codes in a source file, such as 'src.py'. Then open an Anaconda Prompt (assuming you are using this) and run this command (assure you are in the folder containing the 'src.py' file)

python src.py

This solution has worked for me. You should not run the source file in Spyder IDE or Jupyter Notebook.

Pico answered 28/4, 2022 at 15:38 Comment(0)
S
1

This code may works because it needs some periods time to render the object.

r.html.arender(timeout=0,sleep=20)
Stylographic answered 1/7, 2023 at 10:10 Comment(1)
Please add a citation for this.Tanika

© 2022 - 2024 — McMap. All rights reserved.