簡體   English   中英

Jupyter Notebook RuntimeError:無法在現有事件循環中使用 HTMLSession

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

試圖通過 jupyter notebook 抓取亞馬遜,我的代碼在普通編輯器中運行良好,但由於某種原因在 jupyter notebook 中失敗:(

Python 安裝版本: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.
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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM