【问题标题】:Proxy auth with PhantomJS使用 PhantomJS 进行代理身份验证
【发布时间】:2017-11-24 09:17:33
【问题描述】:

我正在尝试使用 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 和主机名时,这有效,但我需要它在没有它的情况下工作

【问题讨论】:

  • 兄弟,我尝试在 Ubuntu 和 RedHat Linux 上使用 Python Selenium 和 PhantomJS 配置代理......花了 3 天,但代理不起作用......
  • 天哪,我真的希望有办法

标签: python selenium authentication scrapy phantomjs


【解决方案1】:

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

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

我不知道您为什么必须重复凭据。我冒昧地猜测第一个身份验证参数没有作为标头传递给代理,因此您需要手动进行。

【讨论】:

    【解决方案2】:

    您必须使用 WebDriverWait 等待驱动程序加载网站,完成后打印源代码。

    from selenium.webdriver.support.ui import WebDriverWait
    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)
     WebDriverWait(driver, *).until(lambda driver: driver.find_element_by_xpath(*)
     body = driver.page_source
     print body
    

    用秒填充*,您要等待的元素的xpath。

    【讨论】:

    • 谢谢,但仍然没有结果
    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 2019-01-31
    • 2019-01-01
    • 2015-09-15
    • 1970-01-01
    • 2013-06-03
    • 2013-11-17
    • 2010-12-10
    相关资源
    最近更新 更多