如何使用 Python 连接 Tor 浏览器

2025-02-18 09:24:00
admin
原创
28
摘要:问题描述:我正在尝试连接到 Tor 浏览器,但收到一条错误消息,提示“proxyConnectFailure”,我曾多次尝试使用 Tor 浏览器的基础知识进行连接,但都徒劳无功,如果能有任何帮助,生命就可以得到大大的挽救:from selenium import webdriver from selenium...

问题描述:

我正在尝试连接到 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:UsersAdminDesktopTor BrowserBrowserirefox.exe")
profile = FirefoxProfile(r"C:UsersAdminDesktopTor BrowserBrowserTorBrowserDataBrowserprofile.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)

解决方案 1:

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

  • 代码块:

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

torexe = os.popen(r'C:UsersAtechM_03DesktopTor BrowserBrowserTorBrowserTor    or.exe')
profile = FirefoxProfile(r'C:UsersAtechM_03DesktopTor BrowserBrowserTorBrowserDataBrowserprofile.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:UtilityBrowserDriversgeckodriver.exe')
driver.get("http://check.torproject.org")
  • 浏览器快照:

Tor_Python


您可以在如何通过 Selenium 在 Chrome 浏览器中使用 Tor中找到相关讨论

解决方案 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")

解决方案 3:

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

对于最新的 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

相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1325  
  IPD(Integrated Product Development)流程作为一种先进的产品开发管理模式,在众多企业中得到了广泛应用。它涵盖了从产品概念产生到产品退市的整个生命周期,通过整合跨部门团队、优化流程等方式,显著提升产品开发的效率和质量,进而为项目的成功奠定坚实基础。深入探究IPD流程的五个阶段与项目成功之间...
IPD流程分为几个阶段   4  
  华为作为全球知名的科技企业,其成功背后的管理体系备受关注。IPD(集成产品开发)流程作为华为核心的产品开发管理模式,其中的创新管理与实践更是蕴含着丰富的经验和深刻的智慧,对众多企业具有重要的借鉴意义。IPD流程的核心架构IPD流程旨在打破部门墙,实现跨部门的高效协作,将产品开发视为一个整体的流程。它涵盖了从市场需求分析...
华为IPD是什么   3  
  IPD(Integrated Product Development)研发管理体系作为一种先进的产品开发模式,在众多企业的发展历程中发挥了至关重要的作用。它不仅仅是一套流程,更是一种理念,一种能够全方位提升企业竞争力,推动企业持续发展的有效工具。深入探究IPD研发管理体系如何助力企业持续发展,对于众多渴望在市场中立足并...
IPD管理流程   3  
  IPD(Integrated Product Development)流程管理旨在通过整合产品开发流程、团队和资源,实现产品的快速、高质量交付。在这一过程中,有效降低成本是企业提升竞争力的关键。通过优化IPD流程管理中的各个环节,可以在不牺牲产品质量和性能的前提下,实现成本的显著降低,为企业创造更大的价值。优化产品规划...
IPD流程分为几个阶段   4  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用