代理:Selenium + Python、Firefox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18719980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:35:42  来源:igfitidea点击:

Proxy: Selenium + Python, Firefox

pythonfirefoxseleniumproxy

提问by Michele Spina

How can I redirect the traffic of Firefox launched by Selenium in Python to a proxy? I have used the solutions suggested on the web but they don't work!

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

I have tried:

我试过了:

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)

回答by amitdatta

You need to import the following:

您需要导入以下内容:

from selenium.webdriver.common.proxy import Proxy, ProxyType

Then setup the proxies:

然后设置代理:

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

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

Then call the webdriver.Firefox() function as follows:

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

driver = webdriver.Firefox(proxy=proxy)
driver.get("http://www.google.com")

Don't remember where exactly I found this solution, but its out there somewhere. Will surely provide a link if I find it again. Just took this part out of my code.

不记得我在哪里找到了这个解决方案,但它在某个地方。如果我再次找到它,肯定会提供一个链接。只是从我的代码中删除了这部分。

回答by m3nda

Your problem is on the driver init. Try webdriver = webdriver.Firefox(firefox_profile=profile)All the other code is OK and you can also remove profile.update_preferences()line.

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

I found YOUR solution with a 2 minute google search. How much time did you spend waiting for? :D

我通过 2 分钟的谷歌搜索找到了您的解决方案。你花了多少时间等待?:D

Your problem is that you maybe read code from other langs but Python. Replace this webdriver.Firefox(profile)with webdriver.Firefox(firefox_profile=profile).

您的问题是您可能从 Python 之外的其他语言中读取代码。将其替换webdriver.Firefox(profile)webdriver.Firefox(firefox_profile=profile).

Your code should be:

你的代码应该是:

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)

回答by OceanFire

Try this for firefox/geckodriver:

在 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)

And for Chrome you can use:

对于 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)