python - Selenium 代理设置
问题描述
我需要在 Python 中通过代理使用 Selenium。我的私人代理类型是:user:password:ip:port
. 下面的代码有效,但它没有user
andpassword
选项。有人可以更新此代码吗?谢谢。
from selenium import webdriver
import time
"Define Both ProxyHost and ProxyPort as String"
ProxyHost = "ip"
ProxyPort = "port"
def ChangeProxy(ProxyHost ,ProxyPort):
"Define Firefox Profile with you ProxyHost and ProxyPort"
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", ProxyHost )
profile.set_preference("network.proxy.http_port", int(ProxyPort))
profile.update_preferences()
return webdriver.Firefox(firefox_profile=profile)
def FixProxy():
"Reset Firefox Profile"
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 0)
return webdriver.Firefox(firefox_profile=profile)
driver = ChangeProxy(ProxyHost ,ProxyPort)
driver.get("http://whatismyipaddress.com")
time.sleep(5)
driver = FixProxy()
driver.get("http://whatismyipaddress.com")
解决方案
定义你的属性:
username = "your_username_here"
password = "your_password_here"
然后将这些添加到您的偏好中:
profile.set_preference("network.proxy.socks_username", username)
profile.set_preference("network.proxy.socks_password", password)
另一种方法:
profile.set_preference('network.http.phishy-userpass-length', 255)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://username:password@somewebsite.com:port/")
推荐阅读
- vmware-clarity - 是否可以为数据网格中的预定义过滤器指定占位符文本?
- python - 基于时差的 pd.merge_asof() 不合并所有值 - 熊猫
- node.js - EJS 包括嵌套目录中相对于设置视图路径的部分
- c# - .Net 程序未获得经过验证的服务器证书
- java - java.lang.OutOfMemoryError: 超出 GC 开销限制:应用程序部署在 weblogic 中
- rpm - 创建解压文件的 rpm 包
- php - PhpStorm - 将 SQL 模式与项目源相关联
- php - 将可选 SQL 传递给准备好的语句
- python - 在特定列的每个工作表中从 Excel 工作簿中读取值
- javascript - 如何根据曲线上的当前时间计算圆的位置