如何在 python webdriver 中为 chrome 设置代理?

2022-01-16 00:00:00 python proxy webdriver google-chrome

问题描述

我正在使用此代码:

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "proxy.server.address")
profile.set_preference("network.proxy.http_port", "port_number")
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)

在 python webdriver 中为 FF 设置代理.这适用于FF.如何在 Chrome 中设置这样的代理?我发现这个 exmaple 但不是很有帮助.当我运行脚本时没有任何反应(Chrome 浏览器未启动).

to set proxy for FF in python webdriver. This works for FF. How to set proxy like this in Chrome? I found this exmaple but is not very helpful. When I run the script nothing happens (Chrome browser is not started).


解决方案

from selenium import webdriver

PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)

chrome = webdriver.Chrome(options=chrome_options)
chrome.get("http://whatismyipaddress.com")

相关文章