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

标签 python selenium proxy tor firefox-profile

我正在尝试连接到 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")
    
  • 浏览器快照:

Tor_Python


You can find a relevant discussion in How to use Tor with Chrome browser through Selenium

关于python - 如何使用 Python 连接到 Tor 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163177/

相关文章:

python - 列表到元组组

python - 在 python 中为 **https** 连接指定具有用户名和密码的代理的最佳方法是什么?

c# cefsharp 浏览器尝试设置代理

java - 如何使用 WebDriver 获取任何 Web 应用程序前端数据的加载时间?

python - 将 Selenium 与 Scrapy 集成

Python/ Selenium : Whitespace issue when retrieving text content from XPath (normalize-space)

java - 带有 XMLEncoder/Decoder 的序列化代理

python - 未知类型名称 'glp_long' (mac osx python, pyglpk)

python - 如果我想要我的对象的非递归深拷贝,我应该在 Python 中重写 copy 还是 deepcopy?

python - 在 Python 列表中查找元素的有效方法?