59

我正在使用这段代码:

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 中设置这样的代理?我找到了这个例子,但不是很有帮助。当我运行脚本时,没有任何反应(Chrome 浏览器未启动)。

4

7 回答 7

111
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")
于 2012-08-06T02:00:23.817 回答
16

它为我工作...

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=http://%s' % PROXY)

chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
于 2014-06-12T11:30:40.353 回答
8

我有同样的问题。ChromeOptions 非常奇怪,因为它没有像您想象的那样与所需的功能集成。我忘记了确切的细节,但基本上 ChromeOptions 会根据您是否传递了所需的功能字典,将某些值重置为默认值。

我做了以下猴子补丁,它允许我指定自己的 dict 而不必担心 ChromeOptions 的复杂性

更改 /selenium/webdriver/chrome/webdriver.py 中的以下代码:

def __init__(self, executable_path="chromedriver", port=0,
             chrome_options=None, service_args=None,
             desired_capabilities=None, service_log_path=None, skip_capabilities_update=False):
    """
    Creates a new instance of the chrome driver.

    Starts the service and then creates new instance of chrome driver.

    :Args:
     - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
     - port - port you would like the service to run, if left as 0, a free port will be found.
     - desired_capabilities: Dictionary object with non-browser specific
       capabilities only, such as "proxy" or "loggingPref".
     - chrome_options: this takes an instance of ChromeOptions
    """
    if chrome_options is None:
        options = Options()
    else:
        options = chrome_options

    if skip_capabilities_update:
        pass
    elif desired_capabilities is not None:
        desired_capabilities.update(options.to_capabilities())
    else:
        desired_capabilities = options.to_capabilities()

    self.service = Service(executable_path, port=port,
        service_args=service_args, log_path=service_log_path)
    self.service.start()

    try:
        RemoteWebDriver.__init__(self,
            command_executor=self.service.service_url,
            desired_capabilities=desired_capabilities)
    except:
        self.quit()
        raise 
    self._is_remote = False

所有改变的是“skip_capabilities_update”kwarg。现在我这样做是为了设置我自己的字典:

capabilities = dict( DesiredCapabilities.CHROME )

if not "chromeOptions" in capabilities:
    capabilities['chromeOptions'] = {
        'args' : [],
        'binary' : "",
        'extensions' : [],
        'prefs' : {}
    }

capabilities['proxy'] = {
    'httpProxy' : "%s:%i" %(proxy_address, proxy_port),
    'ftpProxy' : "%s:%i" %(proxy_address, proxy_port),
    'sslProxy' : "%s:%i" %(proxy_address, proxy_port),
    'noProxy' : None,
    'proxyType' : "MANUAL",
    'class' : "org.openqa.selenium.Proxy",
    'autodetect' : False
}

driver = webdriver.Chrome( executable_path="path_to_chrome", desired_capabilities=capabilities, skip_capabilities_update=True )
于 2014-06-16T05:38:00.550 回答
7

这很简单!

首先,定义您的代理网址

proxy_url = "127.0.0.1:9009"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_url,
    'sslProxy': proxy_url,
    'noProxy': ''})

然后,创建 chrome 功能设置并为其添加代理

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

最后,创建 webdriver 并传递所需的功能

driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")

总而言之,它看起来像这样

from selenium import webdriver
from selenium.webdriver.common.proxy import *

proxy_url = "127.0.0.1:9009"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_url,
    'sslProxy': proxy_url,
    'noProxy': ''})

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")
于 2022-02-01T08:33:16.017 回答
5

这对我来说就像一个魅力:

proxy = "localhost:8080"
desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()
desired_capabilities['proxy'] = {
    "httpProxy": proxy,
    "ftpProxy": proxy,
    "sslProxy": proxy,
    "noProxy": None,
    "proxyType": "MANUAL",
    "class": "org.openqa.selenium.Proxy",
    "autodetect": False
}
于 2019-10-30T13:27:26.747 回答
4

对于那些询问如何在需要身份验证的 chrome 中设置代理服务器的人来说,应该遵循这些步骤。

  1. 在您的项目中创建一个 proxy.py 文件,使用此代码
    并在 每次需要时从 proxy.py调用 proxy_chrome 。您需要传递代理服务器、端口和用户名密码等参数进行身份验证。
于 2018-07-12T06:15:53.580 回答
-14
from selenium import webdriver
from selenium.webdriver.common.proxy import *

myProxy = "86.111.144.194:3128"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy':''})

driver = webdriver.Firefox(proxy=proxy)
driver.set_page_load_timeout(30)
driver.get('http://whatismyip.com')
于 2013-12-03T19:58:19.700 回答