Python requests_html:Socks5h 代理在调用“render()”时不起作用

问题描述

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

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

实际上,我尝试使用以下代码连接到 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 站点时的错误消息:

回溯(最近一次调用最后一次):文件“requests_html_01.py”,行 12、在 content.render() # 渲染 javascript 文件 "/home/testuser/.local/lib/python3.6/site-packages/requests_html.py",第 598 行,在渲染中 内容,结果,页面 = self.session.loop.run_until_complete(self._async_render(url=self.url,脚本=脚本,睡眠=睡眠,等待=等待,内容=self.html, 重新加载=重新加载,向下滚动=向下滚动,超时=超时, keep_page=keep_page)) 文件 “/usr/lib/python3.6/asyncio/base_events.py”,第 484 行,在 运行直到完成 返回 future.result() 文件“/home/testuser/.local/lib/python3.6/site-packages/requests_html.py”, 第 512 行,在 _async_render 中 await page.goto(url,options={'timeout': int(timeout * 1000)}) 文件 "/home/testuser/.local/lib/python3.6/site-packages/pyppeteer/page.py",第 879 行,在 goto 中 引发 PageError(result) pyppeteer.errors.PageError: net::ERR_INTERNET_DISCONNECTED at https://www.bbcnewsv2vjtpsuy.onion/

解决方法

抱歉我的回答。 requests_html 在内部使用 pyppetter,这个代理问题取决于 pyppeteer。目前requests_html好像没有传递代理信息,所以pyppeteer没有使用代理。 根据下面的github页面,这个问题似乎将来会得到解决。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...