5

我正在尝试连接到 Tor 浏览器,但收到一条错误消息,指出“proxyConnectFailure”任何想法我已经尝试过多次尝试了解 Tor 浏览器的基础知识以使其连接,但如果有任何帮助可以挽救生命,那么一切都是徒劳的:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary


binary = FirefoxBinary(r"C:\Users\Admin\Desktop\Tor Browser\Browser\firefox.exe")
profile = FirefoxProfile(r"C:\Users\Admin\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default")

# Configured profile settings.

proxyIP = "127.0.0.1"
proxyPort = 9150

proxy_settings = {"network.proxy.type":1,
    "network.proxy.socks": proxyIP,
    "network.proxy.socks_port": proxyPort,
    "network.proxy.socks_remote_dns": True,
}
driver = webdriver.Firefox(firefox_binary=binary,proxy=proxy_settings)

def interactWithSite(driver):

    driver.get("https://www.google.com")    
    driver.save_screenshot("screenshot.png")

interactWithSite(driver)
4

3 回答 3

10

要通过FirefoxProfile连接到Tor 浏览器,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    import os
    
    torexe = os.popen(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    profile = FirefoxProfile(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.socks', '127.0.0.1')
    profile.set_preference('network.proxy.socks_port', 9050)
    profile.set_preference("network.proxy.socks_remote_dns", False)
    profile.update_preferences()
    driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get("http://check.torproject.org")
    
  • 浏览器快照:

Tor_Python


您可以在How to use Tor with Chrome browser through Selenium中找到相关讨论

于 2018-12-27T09:35:23.387 回答
2

我想通过添加 Linux 对应来扩展 @DebanjanB 答案:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os

torexe = os.popen('some/path/tor-browser_en-US/Browser/start-tor-browser') 
# in my case, I installed it under a folder tor-browser_en-US after
# downloading and extracting it from 
# https://www.torproject.org/download/ for linux

profile = FirefoxProfile(
    'some/path/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()

firefox_options = webdriver.FirefoxOptions()
firefox_options.binary_location = '/usr/bin/firefox' 
# /usr/bin/firefox is default location of firefox - for me anyway

driver = webdriver.Firefox(
    firefox_profile=profile, options=firefox_options, 
    executable_path='wherever/you/installed/geckodriver') 
# I keep my geckodriver(s) in a special folder sorted by versions.
# Geckodriver downloadable here: 
# https://github.com/mozilla/geckodriver/releases/
driver.get("http://check.torproject.org")
于 2021-04-12T18:22:38.537 回答
1

在打开点洋葱站点的情况下,经过验证的答案不起作用(我相信这与不允许访问普通 firefox 的 tor 网络有关)。

至于最新的 tor 浏览器(来自 tor 浏览器包),使用 selenium 启动它会导致一些错误,由于浏览器无法启动 tor 代理本身导致代理和超时错误(不管 tor 代理是由 python 启动还是手动启动或根本没有开始)。这也可能是由于端口 9050 或 9150 被 tor 代理使用并且对浏览器的 tor 实例不可用,但这并不能解释当没有 tor 代理实例运行时导致的错误。

我找到的解决方案是正常启动 tor 代理,手动或使用os.popen("tor.exe")和配置 tor 浏览器以不启动 tor 代理。这是代码:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

os.popen(r'e:\\bla\\bla\\bla\\tor\\Tor\\tor.exe')

binary=FirefoxBinary(r'e:\\bla\\bla\\bla\\Tor Browser\\Browser\\firefox.exe')
fp=FirefoxProfile(r'e:\\foo\\bar\\bla\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default')

fp.set_preference('extensions.torlauncher.start_tor',False)#note this
fp.set_preference('network.proxy.type',1)
fp.set_preference('network.proxy.socks', '127.0.0.1')
fp.set_preference('network.proxy.socks_port', 9050)
fp.set_preference("network.proxy.socks_remote_dns", True)
fp.update_preferences()

driver = webdriver.Firefox(firefox_profile=fp,firefox_binary=binary)
driver.get("http://check.torproject.org")
driver.get('https://www.bbcnewsv2vjtpsuy.onion/')

*第 10 行的注释fp.set_preference('extensions.torlauncher.start_tor',False) 用于将 tor 配置为不启动它自己的 tor 实例,以便它使用上面启动的代理配置和 tor 实例。

lo and behold as the tbb starts working like normal firefox bot browser

于 2021-09-12T23:16:15.550 回答