Skip to main content
 首页 » 编程设计

python之使用 PhantomJS 进行代理身份验证

2025年02月15日8over140

我正在尝试使用 PhantomJS 和 Python 对代理服务器进行身份验证。 这是我的

service_args = [ 
         '--proxy=http://us-ny.proxymesh.com:31280', 
         '--proxy-type=http', 
 
 ] 
 
 authentication_token = "Basic " + base64.b64encode(b'username:pass') 
 
 capa = DesiredCapabilities.PHANTOMJS 
 capa['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token 
 
 driver = webdriver.PhantomJS( desired_capabilities=capa, service_args=service_args) 
 
 driver.get(request.url) 
 body = driver.page_source 
 print body 

这只会打印出来

<html><head></head><body></body></html>

澄清一下,当我将我的 IP 添加到代理服务器时,这会起作用 - 已验证的 IP 和主机名,但我需要它在没有它的情况下工作

请您参考如下方法:

这是我的有效解决方案。我最终需要在 service_args 和 proxy-auth header 中传递凭据。

service_args = [ 
    "--ignore-ssl-errors=true", 
    "--ssl-protocol=any", 
    "--proxy={}".format(proxy), 
    "--proxy-type=http", 
] 
 
caps = DesiredCapabilities.PHANTOMJS 
 
authentication_token = "Basic " + base64.b64encode(b'{}:{}'.format(username, password)) 
 
caps['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token 
 
self.driver = webdriver.PhantomJS( 
        service_args=service_args, 
        desired_capabilities=caps, 
        executable_path="./phantomjs-2.1.1-linux-x86_64/bin/phantomjs") 

代理结构定义为http://username:password@domain:port

我不知道你为什么要重复凭据。我猜测第一个身份验证参数不会作为 header 传递给代理,因此您需要手动执行这两项操作。