python-3.x - Selenium WebdDriverException 使用 Tor
问题描述
我正在尝试使用 tor 构建一个抓取脚本。我已经在我的 WSL Ubuntu 上安装了 tor 和 firefox,并且我还确保取消注释 torrc 文件中的行。但是当我尝试运行我的代码时:
from stem import Signal
from stem.control import Controller
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
# signal TOR for a new connection
def switchIP():
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
# get a new selenium webdriver with tor as the proxy
def my_proxy(PROXY_HOST,PROXY_PORT):
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.socks",PROXY_HOST)
fp.set_preference("network.proxy.socks_port",int(PROXY_PORT))
fp.update_preferences()
options = Options()
options.headless = True
return webdriver.Firefox(options=options, firefox_profile=fp)
for x in range(10):
proxy = my_proxy("127.0.0.1", 9050)
proxy.get("https://whatsmyip.com/")
html = proxy.page_source
soup = BeautifulSoup(html, 'lxml')
print(soup.find("span", {"id": "ipv4"}))
print(soup.find("span", {"id": "ipv6"}))
switchIP()
我得到错误:
WebDriverException: Message: Reached error page: about:neterror?e=proxyConnectFailure&u=https%3A//whatsmyip.com/&c=UTF-8&d=Firefox%20is%20configured%20to%20use%20a%20proxy%20server%20that%20is%20refusing%20connections.
我究竟做错了什么?我试图连接,https://amazon.com
但后来它进入了一个无限循环。
编辑:如果我不从cmd启动tor,连接也会被拒绝吗?这正常吗?我可以从 python 脚本本身开始吗?
解决方案
在我重新启动 WSL 后它起作用了。只是确保 torrc 文件没问题
推荐阅读
- scala - 异常检测 Deequ 库中的直方图
- scanf - 在 scanf() 之后使用 Puts() 和 Gets() 不起作用
- ruby - 如何检查用户输入是否包含数字?在红宝石
- ajax - 无法使用 AJAX 和 AWS API Gateway 发布到 AWS Lambda 函数
- python - AWS API 网关连接到私有端点的 500 错误
- django - 使用诸如“collectstatic”之类的东西填充媒体资产文件
- ruby - Ruby 输出(字符串和整数)
- c++ - 使用 mbedTLS 从服务器中提取公共证书链并存储为字符串
- reactjs - 无法删除屏幕底部的白条
- javascript - 使用 Next.js 和 TS 登录 Facebook SDK