代理:Selenium + Python,Firefox

Mic*_*ina 10 python firefox proxy selenium

如何将Selenium在Python中启动的Firefox流量重定向到代理?我使用过网络上建议的解决方案,但它们不起作用!

我试过了:

profile = webdriver.FirefoxProfile() 
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "54.213.66.208")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences() 
driver = webdriver.Firefox(profile)
Run Code Online (Sandbox Code Playgroud)

ami*_*tta 15

您需要导入以下内容:

from selenium.webdriver.common.proxy import Proxy, ProxyType
Run Code Online (Sandbox Code Playgroud)

然后设置代理:

myProxy = "xx.xx.xx.xx:xxxx"

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy': '' # set this value as desired
    })
Run Code Online (Sandbox Code Playgroud)

然后调用webdriver.Firefox()函数,如下所示:

driver = webdriver.Firefox(proxy=proxy)
driver.get("http://www.google.com")
Run Code Online (Sandbox Code Playgroud)

不记得我在哪里找到了这个解决方案,但它在某处.如果再找到它,肯定会提供一个链接.刚从我的代码中取出这部分.

  • 需要注意的是:`myProxy` 必须是 `IPAddress:port` 或 `hostname:port`,其中主机名不包含 _"http://"_ 前缀 (6认同)
  • “noProxy”是什么意思?它的价值的例子是什么? (2认同)

m3n*_*nda 7

您的问题出在驱动程序init上.尝试webdriver = webdriver.Firefox(firefox_profile=profile)所有其他代码都没问题,你也可以删除profile.update_preferences()行.

我用2分钟的谷歌搜索找到了你的解决方案.你花了多少时间等待?:d

你的问题是你可能从其他语言中读取代码而不是Python.替换webdriver.Firefox(profile)webdriver.Firefox(firefox_profile=profile).

你的代码应该是:

profile = webdriver.FirefoxProfile() 
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "54.213.66.208")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences() 
driver = webdriver.Firefox(firefox_profile=profile)
Run Code Online (Sandbox Code Playgroud)


Oce*_*ire 5

对于Firefox / Geckodriver,请尝试以下操作:

proxy = "212.66.117.168:41258"

firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

firefox_capabilities['proxy'] = {
    "proxyType": "MANUAL",
    "httpProxy": proxy,
    "ftpProxy": proxy,
    "sslProxy": proxy
}

driver = webdriver.Firefox(capabilities=firefox_capabilities)
Run Code Online (Sandbox Code Playgroud)

对于Chrome,您可以使用:

proxy = "212.66.117.168:41258"
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = proxy
prox.socks_proxy = proxy
prox.ssl_proxy = proxy

capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
Run Code Online (Sandbox Code Playgroud)