如何使用 Python 连接 Tor 浏览器
- 2025-02-18 09:24:00
- admin 原创
- 30
问题描述:
我正在尝试连接到 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 BrowserBrowserirefox.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")
浏览器快照:
您可以在如何通过 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
相关推荐
热门文章
项目管理软件有哪些?
- 2025年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)
热门标签
云禅道AD