簡體   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