使用selenium / python设置代理

时间:2016-10-18 16:29:59

标签: python selenium proxy

我正在使用硒与python。 我需要配置一个代理。

它适用于HTTP但不适用于HTTPS。

我使用的代码是:

# configure firefox
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", '11.111.11.11')
profile.set_preference("network.proxy.http_port", int('80'))
profile.update_preferences()

# launch
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://www.iplocation.net/find-ip-address')

另外。有没有办法让我完全阻止来自我的IP的任何传出流量,并将其仅限于代理IP,这样我就不会意外地从代理切换到直接连接而弄乱测试/统计数据?

任何提示都会有所帮助! 谢谢:))

1 个答案:

答案 0 :(得分:1)

查看browsermob proxy以设置代理,以便与selenium

一起使用
from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)


proxy.new_har("google")
driver.get("http://www.google.co.uk")
proxy.har # returns a HAR JSON blob

server.stop()
driver.quit()

您可以使用RemoteServer类的远程代理服务器。

  

我有办法完全阻止来自我的IP的任何传出流量并将其仅限制为代理IP

是的,只需查看如何为您正在使用的操作系统设置代理。请谨慎使用,因为某些操作系统会根据特定条件忽略代理规则,例如,如果使用VPN连接。

相关问题