使用selenium / python设置代理

4szc88ey  于 12个月前  发布在  Python
关注(0)|答案(2)|浏览(143)

我正在使用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,这样我就不会意外地混乱测试/统计从代理切换到直接连接?
任何提示将帮助!Thanks:)

4ktjp1zp

4ktjp1zp1#

查看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连接)忽略代理规则。

uurity8g

uurity8g2#

这就是它的答案。你可以试试这个。
对于 PROXY,您可以使用任何带端口的代理主机。
https://stackoverflow.com/a/48498403/5533485

相关问题