繁体   English   中英

使用Selenium / Python设置代理

[英]Setting up proxy with selenium / python

我在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,这样我就不会因意外从代理切换到直接连接而无意中弄乱了测试/统计信息?

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

检出browsermob代理以设置与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连接)将忽略代理规则。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM