在 Google Cloud Engine 中使用 Chromedriver 代理

Séb*_*eer 7 python selenium-chromedriver google-cloud-platform

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

我尝试了许多建议的解决方案(见下文),但每次 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()


Run Code Online (Sandbox Code Playgroud)

尝试 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()

Run Code Online (Sandbox Code Playgroud)

尝试 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()
Run Code Online (Sandbox Code Playgroud)

他们都不会使用所需的 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))

Run Code Online (Sandbox Code Playgroud)

谢谢阅读 !

Lif*_*lex 7

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

您的代理是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/')
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

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

在此处输入图片说明

当我在 5 分钟后做同样的检查时,代理在 HTTP 上启动,但有延迟问题。

在此处输入图片说明

如果我从免费代理网站选择了另一个代理:

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/')

Run Code Online (Sandbox Code Playgroud)

CloudFlare连接到网站时出现挑战页面whatismyip.

在此处输入图片说明

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

在此处输入图片说明

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

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