gpt4 book ai didi

python - 在 Google Cloud Engine 中使用 Chromedriver 代理

转载 作者:行者123 更新时间:2023-12-04 04:30:16 26 4
gpt4 key购买 nike

我正在尝试在带有 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()


尝试 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()
他们都不会访问具有所需 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 ,如下图所示。
enter image description here
当我测试这段代码时:

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 浏览器打开,但不显示任何内容。
enter image description here
如果我检查地址 207.157.25.44:80通过在线 proxy checker服务,我得到的结果喜忧参半。
下图显示代理未响应任何查询类型(HTTP、HTTPS、SOCKS4、SOCKS5)。
enter image description here
当我在 5 分钟后做同样的检查时,代理在 HTTP 上启动,但有延迟问题。
enter image description here
如果我从 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. enter image description here
但是如果我在网站 nordvpn.com/what-is-my-ip 上尝试相同的代理我得到了代理的 IP 地址。
enter image description here
我强烈建议多次测试任何免费代理 IP 地址,以查看该地址是否存在任何类型的问题。此外,您需要在代码中添加一些错误处理以在代理脱机时捕获问题,因为它们随时可能下降。
如果您需要使用代理,我强烈建议您使用商业代理服务,因为它们比免费代理服务更可靠。
  • oxylabs.io
  • bright data
  • 关于python - 在 Google Cloud Engine 中使用 Chromedriver 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68259141/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com