【问题标题】:"SocketError: [Errno 61] Connection refused" Selenium with TOR“SocketError: [Errno 61] Connection denied” Selenium with TOR
【发布时间】:2019-12-12 03:19:11
【问题描述】:

所以我使用 TOR 来更改我的 webdriver 的代理和 IP 地址。这是代码。已安装所有依赖项(包括 Geckodriver 和最新版本的 Firefox)。

from stem import Signal
from stem.control import Controller
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup

def switchIP():
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate()
        controller.signal(Signal.NEWNYM)


def my_proxy(PROXY_HOST,PROXY_PORT):
    fp = webdriver.FirefoxProfile()
    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()

感谢您的帮助, 阿拉夫。

【问题讨论】:

  • 连接被拒绝错误出现在哪里或堆栈跟踪是什么?你能确认 Tor 的 SOCKS 代理正在监听 9050 端口,而控制器正在监听 9051 端口吗?

标签: python selenium proxy tor firefox-profile


【解决方案1】:

此错误消息...

SocketError: [Errno 61] Connection refused

...表示连接被服务器拒绝。


由于以下几个原因,可能会出现此错误:

  1. 防火墙阻止请求/响应。
  2. 代理配置不正确。
  3. 代理类型无效。
  4. 代理主机无效。
  5. 代理端口无效。等等

这个用例

但是我没有看到您的代码块有任何问题,但是在使用 能够更改代理和 IP 地址时,您需要使用 @ 启动 torexe 应用程序987654326@命令如下(以 OS为例):

import os

torexe = os.popen(r'C:\Users\user_name\path\to\Tor Browser\Browser\TorBrowser\Tor\tor.exe')

参考文献

您可以在以下位置找到相关讨论:

【讨论】:

  • 我无法运行 .exe 文件,因为我在 MAC os 上。有没有其他方法可以做到以上几点
  • @AaravM4 似乎popen() 确实存在于 MAC OSX 中。尝试使用例如的绝对路径/path/to/tor
猜你喜欢
  • 2019-10-06
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
  • 2021-09-16
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多