在 Google Cloud Engine 中使用 Chromedriver 代理

Using proxy with Chromedriver within Google Cloud Engine

我正在尝试在 Google Cloud Engine 中使用 chromedriver 代理。

我尝试了很多建议的解决方案(见下文),但每次 IP 都是 Google 服务器上的 IP。

尝试 1:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--ignore-certificate-errors")

myproxy = '207.157.25.44:80'
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = myproxy
prox.ssl_proxy = myproxy

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

driver = webdriver.Chrome(options=chrome_options, 
    executable_path="/user/sebastien/chromedriver", 
    desired_capabilities=capabilities)
driver.get("https://www.whatismyip.com/")
get_location()


尝试 2:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--ignore-certificate-errors")

myproxy = '207.157.25.44:80'
prefs = {}
prefs["network.proxy.type"] = 1
prefs["network.proxy.http"] = myproxy
prefs["network.proxy.ssl"] = myproxy

chrome_options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(options=chrome_options, 
    executable_path="/user/sebastien/chromedriver")
driver.get("https://www.whatismyip.com/")
get_location()

尝试 3:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--ignore-certificate-errors")

myproxy = '207.157.25.44:80'
chrome_options.add_argument("--proxy-server=http://%s" % myproxy)

driver = webdriver.Chrome(options=chrome_options,
    executable_path="/user/sebastien/chromedriver")
driver.get("https://www.whatismyip.com/")
get_location()

None 他们将到达具有所需 IP 的网站。

同样,当 运行 GCP Compute Engine、Canonical、Ubuntu、16.04 LTS、amd64 xenial 上的代码时会发生此问题。

下面函数测试IP:

import json
from urllib.request import urlopen

def get_location(ip=False):
    if ip:
        html = urlopen(f"http://ipinfo.io/{str(ip).split(':')[0]}/json")
    else:
        html = urlopen("http://ipinfo.io/json")

    data = json.loads(html.read().decode('utf-8'))
    IP = data['ip']
    org = data['org']
    city = data['city']
    country = data['country']
    region = data['region']

    print('IP detail')
    print('IP : {4} \nRegion : {1} \nCountry : {2} \nCity : {3} \nOrg : {0}'.format(org, region, country, city, IP))

感谢阅读!

我认为您遇到的问题与您的代码实现无关。我确定您遇到的问题与您使用免费代理有关。这些类型的代理 因连接问题而臭名昭著,例如与延迟相关的超时。此外,这些站点也可能是间歇性的,这意味着它们随时可能出现故障。有时这些网站会被滥用,因此可能会被屏蔽。

您的代理是207.157.25.44:80,如下图所示。

当我测试这段代码时:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

proxy_server = '207.157.25.44:80'

chrome_options = Options()
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument('--proxy-server=%s' % proxy_server)

# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

driver.get('https://www.whatismyip.com/')

Chrome浏览器打开,但不显示任何内容。

如果我通过在线 proxy checker 服务检查地址 207.157.25.44:80,我会得到不同的结果。

下图显示代理未响应任何查询类型(HTTP、HTTPS、SOCKS4、SOCKS5)。

当我在 5 分钟后执行相同的检查时,代理在 HTTP 上启动,但存在延迟问题。

如果我从 free proxy website 中选择了另一个代理:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

proxy_server = '47.184.133.79:3128'

chrome_options = Options()
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument('--proxy-server=%s' % proxy_server)

# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

driver.get('https://www.whatismyip.com/')

我在连接到网站时收到 CloudFlare 挑战页面 whatismyip.

但是如果我在网站上尝试相同的代理 nordvpn.com/what-is-my-ip 我会得到代理的 IP 地址。

我强烈建议多次测试任何免费代理 IP 地址,看看该地址是否有任何类型的问题。此外,您需要在代码中添加一些错误处理以在代理离线时捕获问题,因为它们随时可能掉线。

如果您需要使用代理,我强烈建议您使用商业代理服务,因为它们比免费代理服务更可靠。