Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Every time while i call r.html.render() , it tell me error "This event loop is already running" #140

Closed
zhang-win opened this issue Mar 16, 2018 · 13 comments

Comments

@zhang-win
Copy link

I wrote code like this:

from requests_html import HTMLSession
session = HTMLSession()
r = session.get(url)
r.html.links

I used this to get data from website, and found it had to load javascript, so i wrote the following:

r.html.render()

it gave message like the below:

RuntimeError: This event loop is already running

but i checked the html resource, it did not change.
so i tried again and again, but it did report the same error. And the chromium started by it stop to response.
These code run on jupyter notebook OS: mac OSX 10.12.6 python: 3.6.2

I don't know what happened and how to resolve it.

@mapio
Copy link

mapio commented Mar 16, 2018

Same here, happens in Jupyter, not if running from the Python prompt.

@oldani
Copy link
Member

oldani commented Mar 16, 2018

This is due to jupyter use an event loop under the hood and request-html calls loop.run_until_complete which rise that exception when the loop is already running; taking a look into.

@oldani
Copy link
Member

oldani commented Mar 17, 2018

I said we wait until async version go out (almost there). Right now schedule a coroutine and wait for its result is kind of tricky. So far r.html.render() cannot be called from an (app|process|script) which have a loop already running.

@kennethreitz
Copy link
Collaborator

This code is not designed to be run from within an existing event loop, currently.

@zhang-win
Copy link
Author

Thanks.

@jschnurr
Copy link

For those discovering this later, you'll find discussion here. The recommended workaround is to use nest_asyncio, which in my limited testing will allow r.html.render() to work in a Jupyter Notebook.

@mamallzipo
Copy link

hi guys when i trying this code >>> r.html.render()
i faced this error
[W:pyppeteer.chromium_downloader] start chromium download.
Download may take a few minutes.
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 193/193 [00:00<?, ?it/s]
[W:pyppeteer.chromium_downloader]
chromium download done.
Traceback (most recent call last):
File "c:/Users/mohamad/Desktop/aa.py", line 6, in
r.html.render()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests_html.py", line 586, in render
self.browser = self.session.browser # Automatically create a event loop and browser
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests_html.py", line 730, in browser
self._browser = self.loop.run_until_complete(super().browser)
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests_html.py", line 714, in browser
self._browser = await pyppeteer.launch(ignoreHTTPSErrors=not(self.verify), headless=True, args=self.__browser_args)
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\launcher.py", line 305, in launch
return await Launcher(options, **kwargs).launch()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\launcher.py", line 119, in init
download_chromium()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\chromium_downloader.py", line 146, in download_chromium
extract_zip(download_zip(get_url()), DOWNLOADS_FOLDER / REVISION)
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\chromium_downloader.py", line 134, in extract_zip
with ZipFile(data) as zf:
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1269, in init
self._RealGetContents()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1336, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

@husanpy
Copy link

husanpy commented Mar 24, 2021

This is due to jupyter use an event loop under the hood and request-html calls loop.run_until_complete which rise that exception when the loop is already running; taking a look into.

is it that I can't use Jupyter if I need the html.render method?

@rosel11078-start
Copy link

hi guys when i trying this code >>> r.html.render()
i faced this error
[W:pyppeteer.chromium_downloader] start chromium download.
Download may take a few minutes.
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 193/193 [00:00<?, ?it/s]
[W:pyppeteer.chromium_downloader]
chromium download done.
Traceback (most recent call last):
File "c:/Users/mohamad/Desktop/aa.py", line 6, in
r.html.render()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests_html.py", line 586, in render
self.browser = self.session.browser # Automatically create a event loop and browser
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests_html.py", line 730, in browser
self._browser = self.loop.run_until_complete(super().browser)
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests_html.py", line 714, in browser
self._browser = await pyppeteer.launch(ignoreHTTPSErrors=not(self.verify), headless=True, args=self.__browser_args)
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\launcher.py", line 305, in launch
return await Launcher(options, **kwargs).launch()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\launcher.py", line 119, in init
download_chromium()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\chromium_downloader.py", line 146, in download_chromium
extract_zip(download_zip(get_url()), DOWNLOADS_FOLDER / REVISION)
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyppeteer\chromium_downloader.py", line 134, in extract_zip
with ZipFile(data) as zf:
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1269, in init
self._RealGetContents()
File "C:\Users\mohamad\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1336, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

@rosel11078-start
Copy link

I post this after 6 days I found solutions, You just need to change the way you're connecting to google because chromiun file is not downloaded correctly in some way you can't reach the ZIP file, I used TOR browser and bypass all connection and them voila chrome zip file is downloading right now it's about 136mb, "r.html.render()" is working right now.

@pako-github
Copy link

I post this after 6 days I found solutions, You just need to change the way you're connecting to google because chromiun file is not downloaded correctly in some way you can't reach the ZIP file, I used TOR browser and bypass all connection and them voila chrome zip file is downloading right now it's about 136mb, "r.html.render()" is working right now.

I face exactly the same issue, but I do not understand your workaround. Could you be more specific?

@jay0x55
Copy link

jay0x55 commented Jul 4, 2021

I post this after 6 days I found solutions, You just need to change the way you're connecting to google because chromiun file is not downloaded correctly in some way you can't reach the ZIP file, I used TOR browser and bypass all connection and them voila chrome zip file is downloading right now it's about 136mb, "r.html.render()" is working right now.

I face exactly the same issue, but I do not understand your workaround. Could you be more specific?

just run your script with torsocks

torsocks my_script.py

or you can set tor socks5 proxy for it

@rosel11078-start
Copy link

rosel11078-start commented Jul 5, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants