如何解决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页面,这个问题似乎将来会得到解决。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。