代理:Selenium + Python、Firefox

18

我如何将用Python中的Selenium启动的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)

可能有多个解决方案在网上。你尝试了什么?(特别是,你尝试过这个吗:http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#firefox ?) - unutbu
我已经尝试过这个: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) - Michele Spina
你的URL是使用http:还是https: - unutbu
1
我执行了这个命令 driver.get("http://whatismyipaddress.com"),但是 IP 地址并不是代理的 IP 地址,而是我的 IP 地址... - Michele Spina
你找到如何使它工作了吗?我也遇到了同样的问题,我正在使用Firefox46。 - salvob
4个回答

22

您需要导入以下内容:

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

然后设置代理:

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

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

然后按以下方式调用webdriver.Firefox()函数:

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

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


我认为你从JAVA独立服务器获取了那段代码,它使用这种方式来查找所需的浏览器功能。这真的有效吗? - m3nda
这是Python的解决方案,不是Java。对我来说绝对有效。 - amitdatta
我的错误。你编写的那个函数方式非常类似于Java示例指南,而Python指南并没有提供所有可能的编码方式,同时支持。也许你是一位专家编码人员,或者你仅仅是这样学习的,但在初学指南中并不存在。我已经寻找了使用带名称的配置文件进行编码的方法,但无法意识到如何编辑当前的随机创建的配置文件,即当前实例。我肯定会测试它。谢谢。 - m3nda
6
需要注意的是:myProxy 必须为 IPAddress:port 或者 hostname:port,其中主机名不包含前缀 "_http://"_。 - salvob
2
noProxy 是什么意思?它的一个示例值是什么? - André C. Andersen
显示剩余2条评论

17

尝试使用火狐浏览器/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)

对于谷歌浏览器,您可以使用:

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)

谢谢。如果您愿意,您也可以使用我的Python库获取免费和新鲜的代理 https://github.com/OceanFireSoftware/python-simple-proxy - OceanFire
这对我有用!!!很多解决方案都缺少firefox_capabilities['marionette'] = True。感谢您的分享! - Clay
这非常实用。经过多次尝试与错误,这是唯一在最新的Firefox / Geckodriver设置中有效的解决方案。因为在firefox 46版本之后,您需要使用geckodriver,并且可以以这种方式设置代理。 - TapanHP
唯一真正有效的解决方案。应标记为“已接受的答案”。谢谢。 - PATAPOsha
对我来说,没有使用 ['marionette'] = True 也可以工作。我猜测这是因为默认情况下它是True =) - PATAPOsha

7
您的问题在于驱动程序的初始化。请尝试使用webdriver = webdriver.Firefox(firefox_profile=profile)。其他代码都没问题,您还可以删除profile.update_preferences()这行代码。
我只用了两分钟的谷歌搜索就找到了您的解决方案。您等了多久? :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)

WebDriverзҡ„__init__ж–№жі•пјҲдҪңдёәFirefoxеҜје…ҘпјүжҺҘеҸ—firefox_profileдҪңдёә第дёҖдёӘеҸӮж•°гҖӮ - Raz
不得不承认,如果您使用的命名变量与第一个“位置参数”不同,那么我们完全在谈论不同的事情。这将类似于运行Firefox("firefox", profile)而没有命名参数。 - m3nda
1
我不确定你在说什么。请查看源代码Firefox(firefox_profile=profile)等同于Firefox(profile) - Raz
@Raz然后解释给OP为什么这对他不起作用,但使用命名变量可以。也许源代码在版本之间发生了变化?我不知道。 - m3nda
1
我从不在未测试代码的情况下回复,这就是我的说法。谢谢你的无端粗鲁。请远离在stackoverflow上发表无用评论。 - m3nda

0

这里是 Selenium 4.x 的代码,要启用代理,您需要设置浏览器选项,这些参数来自底部(ssl 是 https),其余的根据名称都很清楚。为了找到这些参数,我进入了浏览器配置并手动检查选择什么。

我还知道如何在驱动程序中移除机器人检查,为此需要在 rast 上编译您的驱动程序。

def main():
    url = "https://2ip.ru/"

    options = Options()
    options.set_preference("network.proxy.type", 1)
    options.binary_location = r"C:\Program Files\Mozilla Firefox\firefox.exe"
    options.set_preference("network.proxy.http", "xx.xxx.xxx.xxxx")
    options.set_preference("network.proxy.http_port", 8000)
    options.set_preference("network.proxy.ssl", "xx.xxx.xxx.xxx")
    options.set_preference("network.proxy.ssl_port", 8000)

    serviceDriver = Service(
    executable_path=r"C:\Users\User\PycharmProjects\driver\geckodriver.exe")

    driver = webdriver.Firefox(options=options, service=serviceDriver)
    driver.get("https://2ip.ru")

根据目前的写法,你的回答不够清晰。请编辑以添加更多细节,帮助其他人理解这如何回答所提出的问题。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community
那对我没用 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接