Python:带有Tor代理的Selenium不允许连接

时间:2020-04-19 15:11:52

标签: python selenium

当我尝试通过firefox selenium bot连接到tor代理时,出现错误消息“代理服务器拒绝连接”

错误消息:https://i.stack.imgur.com/nmZoK.png

我的代码:

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

firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
binary = FirefoxBinary(r"C:\Program Files\Mozilla Firefox\firefox.exe")

user_agent = "Firefox"
proxy_ip = "127.0.0.1"
proxy_port = 9050

profile = webdriver.FirefoxProfile()

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks_version", 5)
profile.set_preference("network.proxy.socks", proxy_ip)
profile.set_preference("network.proxy.socks_port", proxy_port)
profile.set_preference("network.http.use-cache", False)
profile.set_preference("general.useragent.override", user_agent)
profile.update_preferences()

driver = webdriver.Firefox(firefox_binary=binary,capabilities=firefox_capabilities,firefox_profile=profile)
def interactWithSite(driver):
    driver.get("https://check.torproject.org/")
interactWithSite(driver)

1 个答案:

答案 0 :(得分:0)

解决方案:您需要在后台运行Tor浏览器,才能使用FireFox用户代理/浏览器连接到Tor浏览器网络/代理。