搜索了好几个小时后,我开始认为这是不可能的。
我需要为每次运行使用不同的经过身份验证的(非公共(public))代理通过 selenium 运行 Chrome。
PROXY_IP = "<some IP address>"
UID = "<the user id>"
PWD = "<the password">
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=%s:%s@%s" % (UID,PWD,PROXY_IP))
driver = webdriver.Chrome(executable_path=".\\driver\\chromedriver.exe",
chrome_options=options)
driver.get("<site URL>")
Chrome 将启动并显示错误:
This webpage is not available
ERR_NO_SUPPORTED_PROXIES
如果我使用像这样不需要身份验证的公共(public)代理...
PROXY_IP = "<public proxy IP address>"
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=%s" % PROXY_IP)
driver = webdriver.Chrome(executable_path=".\\driver\\chromedriver.exe",
chrome_options=options)
driver.get("<site URL>")
...它运行良好并在使用代理时显示站点。
我还尝试了在用户 ID 前添加 http://
的变体:
options.add_argument("--proxy-server=http://%s:%s@%s" % (UID,PWD,PROXY_IP))
我进行了广泛的搜索,但没有找到解决方案,这让我相信根本不存在。
我确实找到了这个,但我无法理解它:
selenium chromedriver authentication proxy
不确定 browswermob-proxy
是什么或应该做什么或如何在 Python 中实现和测试。除非绝对必要,否则我讨厌堆积创可贴解决方案。
编辑(21 年 11 月 8 日):
我已经多年不使用 Selenium 了。正因为如此,我现在缺乏上下文(和时间,对不起)来检查提供的较新答案并将一个标记为该问题的解决方案。 SO 是否有一种机制可以用来有效地将这一职能委托(delegate)给可能是该领域专业知识的当前从业者?
最佳答案
要在 python selenium 中使用带有身份验证的代理,您可以使用 seleniumwire .
首先,使用 pip install selenium-wire
然后从 seleniumwire 导入 webdriver 而不是 selenium
from seleniumwire import webdriver
options = {
'proxy': {
'http': 'http://username:password@host:port',
'https': 'https://username:password@host:port',
'no_proxy': 'localhost,127.0.0.1' # excludes
}
}
browser = webdriver.Chrome(path_to_driver, seleniumwire_options=options)
现在您可以像使用 selenium 一样使用浏览器实例:browser.get('https://api.ipify.org')
等等...
关于python - 如何在 selenium chromedriver 中使用经过身份验证的代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30451190/