在 selenium python 中使用代理

Posted

技术标签:

【中文标题】在 selenium python 中使用代理【英文标题】:Using proxies in selenium python 【发布时间】:2021-02-07 06:01:44 【问题描述】:

我正在尝试使用 selenium 使用我找到的代码更改我的 IP 但我收到一个错误: selenium.common.exceptions.WebDriverException:消息:“chromedriver”可执行文件需要在 PATH 中 `从硒导入网络驱动程序

PROXY = "23.23.23.23:3128" # IP:PORT 或 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")`

【问题讨论】:

【参考方案1】:

在这里,我希望它有所帮助:)

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"

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

driver = webdriver.Chrome(desired_capabilities=capabilities)

【讨论】:

您好,感谢您的评论,但它一直给我错误''chrome driver' executable needs to be in PATH'。 试试这个驱动 = webdriver.chrome(executable_path=r"path")【参考方案2】:

我通过将可执行路径添加到 chrome 变量解决了这个错误

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=ipaddress:port')
driver = webdriver.Chrome(executable_path="your executable path",chrome_options=chrome_options)
 driver.get('https://www.myip.com/')

【讨论】:

以上是关于在 selenium python 中使用代理的主要内容,如果未能解决你的问题,请参考以下文章

Selenium 使用 Python:输入/提供 Firefox 的 http 代理密码

代理:Selenium + Python、Firefox

如何在 Python + Selenium 中创建随机用户代理?

selenium 设置代理选项

如何在 Python 上使用 selenium webdriver 和 browsermob 代理捕获网络流量?

Python + Selenium + Firefox 使用代理 auth 的用户名密码授权