Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PhantomJS的代理身份验证_Python_Selenium_Authentication_Scrapy_Phantomjs - Fatal编程技术网

Python 使用PhantomJS的代理身份验证

Python 使用PhantomJS的代理身份验证,python,selenium,authentication,scrapy,phantomjs,Python,Selenium,Authentication,Scrapy,Phantomjs,我正在尝试使用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和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
这只打印出来


只是想澄清一下,当我将我的IP添加到代理服务器(经过身份验证的IP和主机名)时,这会起作用,但我需要它在没有该功能的情况下工作

您必须使用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。

这是我的解决方案。我最终需要在服务参数和代理身份验证头中传递凭据

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@域:端口


我不知道你为什么要重复这些证件。我猜第一个auth参数没有作为头传递给代理,因此您需要手动执行这两项操作。

谢谢,但仍然没有结果Bro我尝试在Ubuntu和RedHat Linux上使用Python Selenium和PhantomJS配置代理。。。花了3天,但代理没有工作…哦,男孩,我真的希望有一个办法