使用selenium / python设置代理

4szc88ey  于 2023-06-29  发布在  Python
关注(0)|答案(2)|浏览(242)

我正在使用python。我需要配置一个代理。
它适用于HTTP,但不适用于HTTPS。
我使用的代码是:

  1. # configure firefox
  2. profile = webdriver.FirefoxProfile()
  3. profile.set_preference("network.proxy.type", 1)
  4. profile.set_preference("network.proxy.http", '11.111.11.11')
  5. profile.set_preference("network.proxy.http_port", int('80'))
  6. profile.update_preferences()
  7. # launch
  8. driver = webdriver.Firefox(firefox_profile=profile)
  9. driver.get('https://www.iplocation.net/find-ip-address')

还有.有没有一种方法可以让我完全阻止任何流出的流量从我的IP和限制它只在代理IP,这样我就不会意外地混乱测试/统计从代理切换到直接连接?
任何提示将帮助!Thanks:)

4ktjp1zp

4ktjp1zp1#

查看browsermob proxy以设置用于selenium的代理

  1. from browsermobproxy import Server
  2. server = Server("path/to/browsermob-proxy")
  3. server.start()
  4. proxy = server.create_proxy()
  5. from selenium import webdriver
  6. profile = webdriver.FirefoxProfile()
  7. profile.set_proxy(proxy.selenium_proxy())
  8. driver = webdriver.Firefox(firefox_profile=profile)
  9. proxy.new_har("google")
  10. driver.get("http://www.google.co.uk")
  11. proxy.har # returns a HAR JSON blob
  12. server.stop()
  13. driver.quit()

您可以使用带有RemoteServer类的远程代理服务器。
有没有一种方法可以完全阻止任何从我的IP传出的流量,并将其限制在代理IP上
是的,只要看看如何为你使用的任何操作系统设置代理。请谨慎使用,因为某些操作系统会根据某些条件(例如,如果使用VPN连接)忽略代理规则。

展开查看全部
uurity8g

uurity8g2#

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

相关问题