Firaki12345
Firaki12345

Reputation: 37

Jupyter Notebook RuntimeError: Cannot use HTMLSession within an existing event loop

trying to scrape amazon through jupyter notebook, my code runs fine through normal editors but fails in jupyter notebook for some reason :(

Python Version Installed : Python 3.9.1

from requests_html import HTMLSession
import nest_asyncio

nest_asyncio.apply()

s = HTMLSession()
r = s.get("https://www.amazon.in/s?k=oneplus&page=1")
r.html.render(sleep=1)

RuntimeError                              Traceback (most recent call last)
<ipython-input-6-9d8324f65180> in <module>
      1 s = HTMLSession()
      2 r = s.get("https://www.amazon.in/s?k=oneplus&page=1")
----> 3 r.html.render(sleep=1)

~\AppData\Roaming\Python\Python39\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 

~\AppData\Roaming\Python\Python39\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.

Upvotes: 1

Views: 4530

Answers (1)

Ajay
Ajay

Reputation: 5347

from requests_html import HTMLSession
import nest_asyncio

nest_asyncio.apply()

s = HTMLSession()
url="https://www.amazon.in/s?k=oneplus&page=1"
r = s.get(url)
ht= r.text
print(ht)

Upvotes: 2

Related Questions