Python requests_html: 调用 "render()" 时 Socks5h 代理不起作用

Python requests_html: Socks5h proxy does not work when calling "render()"

我正在使用“python requests_html”,因为我想获得呈现的 html 源代码。 此外,我想通过 socks5h(Tor) 代理来做到这一点。

所以,我试着写了下面的代码。 但是,一旦调用 render() 函数,就会显示原始 ip 地址。 这似乎是 render() 函数不使用代理设置。

实际上,我尝试使用以下代码连接到 tor bbc 新闻(洋葱域),但失败了,因为那不是 tor 网络。

使用 socks5h 代理渲染有什么好主意吗?

from requests_html import HTMLSession

url = "http://ifconfig.me/ip"
# url = "https://www.bbcnewsv2vjtpsuy.onion/" # bbc news
session = HTMLSession()

proxies = {"http": "socks5h://localhost:9150","https": "socks5h://localhost:9150"}
r = session.get(url, proxies=proxies)
content = r.html
print(content.text) # Tor’s IP will be displayed 

content.render()    # rendering for javascript, etc..
print(content.text) # Raw IP will be displayed

尝试访问 bbcnews 站点时出现错误消息:

Traceback (most recent call last): File "requests_html_01.py", line 12, in content.render() # rendering for javascript File "/home/testuser/.local/lib/python3.6/site-packages/requests_html.py", line 598, in render content, result, page = self.session.loop.run_until_complete(self._async_render(url=self.url, script=script, sleep=sleep, wait=wait, content=self.html, reload=reload, scrolldown=scrolldown, timeout=timeout, keep_page=keep_page)) File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete return future.result() File "/home/testuser/.local/lib/python3.6/site-packages/requests_html.py", line 512, in _async_render await page.goto(url, options={'timeout': int(timeout * 1000)}) File "/home/testuser/.local/lib/python3.6/site-packages/pyppeteer/page.py", line 879, in goto raise PageError(result) pyppeteer.errors.PageError: net::ERR_INTERNET_DISCONNECTED at https://www.bbcnewsv2vjtpsuy.onion/

抱歉自己回答。 requests_html内部使用pyppetter,这个代理问题依赖于pyppeteer。当前requests_html好像没有传递代理信息,所以pyppeteer没有使用代理。 根据后面的github页来看,好像这个问题以后会解决的。