如何使用 Python 连接到 Tor 浏览器

How to connect to Tor browser using Python

我正在尝试连接到 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)

要通过 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")
    
  • 浏览器快照:


You can find a relevant discussion in

我想通过添加 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")

经过验证的答案在打开 dot onion 站点的情况下不起作用(我认为这与不允许访问普通 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