网上搜索了一堆,都说没办法设置或者要用第三方全局代理来转接消息
但在下文中发现wedriver里有代理类,而且有账户密码参数可以输入,只不过跑不通
使用Selenium Chrome进行代理SOCKS 5身份验证-python黑洞网
初步分析如果库中具备代理类的话,应当是可以使用代理的,只不过调用方法有问题
我在使用这段代码跑的时候,分别出现了代理类型无效、需要设置socket版本的异常
代理类型看下如下定义:
DIRECT = ProxyTypeFactory.make(0, 'DIRECT') # Direct connection, no proxy (default on Windows).
MANUAL = ProxyTypeFactory.make(1, 'MANUAL') # Manual proxy settings (e.g., for httpProxy).
PAC = ProxyTypeFactory.make(2, 'PAC') # Proxy autoconfiguration from URL.
RESERVED_1 = ProxyTypeFactory.make(3, 'RESERVED1') # Never used.
AUTODETECT = ProxyTypeFactory.make(4, 'AUTODETECT') # Proxy autodetection (presumably with WPAD).
SYSTEM = ProxyTypeFactory.make(5, 'SYSTEM') # Use system settings (default on Linux).
UNSPECIFIED = ProxyTypeFactory.make(6, 'UNSPECIFIED') # Not initialized (for internal use).
很明显,只有1、2、4有可能,运气比较好,选了1就ok了
提示socket版本问题,确实找了一会,先是跟踪到抛出异常的代码位置,发现没有什么有用信息
于是试着尝试在capbaitlities中添加socksVersion参数,还是报错
再调试代码,发现经过? proxy.add_to_capabilities 后,proxy变成capbaitlities的一个字典成员
预算直接改字典里的socksVersion,结果ok
以下是代码:
def SocketProxyDriver(ProxyIp,us,ps):
from selenium.webdriver import Chrome
from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy = Proxy()
proxy.socksProxy = ProxyIp
proxy.socksUsername = us
proxy.socksPassword = ps
proxy.proxy_type = ProxyType.MANUAL
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
capabilities['proxy']['socksVersion'] = 5
driver = webdriver.Chrome(desired_capabilities=capabilities)#
driver.get("https://baidu.com/")
os.system('pause')
|