【问题标题】:phantomjs + selenium in python proxy-auth not workingpython proxy-auth中的phantomjs + selenium不起作用
【发布时间】:2017-01-11 19:01:04
【问题描述】:

我正在尝试使用 selenium + phantomjs 设置网络抓取代理。我正在使用python。

我在很多地方都看到 phantomjs 中存在一个错误,导致 proxy-auth 不起作用。

from selenium.webdriver.common.proxy import *
from selenium import webdriver
from selenium.webdriver.common.by import By
service_args = [
'--proxy=http://fr.proxymesh.com:31280',
'--proxy-auth=USER:PWD',
'--proxy-type=http',
]

driver = webdriver.PhantomJS(service_args=service_args)
driver.get("https://www.google.com")
print driver.page_source

代理网格建议使用以下内容:

page.customHeaders={'Proxy-Authorization': '基本'+btoa('USERNAME:PASSWORD')};

但我不确定如何将其翻译成 python。

这是我目前拥有的:

from selenium import webdriver
import base64
from selenium.webdriver.common.proxy import *
from selenium import webdriver
from selenium.webdriver.common.by import By

service_args = [
'--proxy=http://fr.proxymesh.com:31280',
'--proxy-type=http',
]

headers = { 'Proxy-Authorization': 'Basic ' +   base64.b64encode('USERNAME:PASSWORD')}

for key, value in enumerate(headers):
    webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.{}'.format(key)] = value

driver = webdriver.PhantomJS(service_args=service_args)
driver.get("https://www.google.com")
print driver.page_source

但它不起作用。

关于如何让它工作的任何建议?

【问题讨论】:

  • 你需要使用 Selenium 和 PhantomJS 吗?对于网络抓取,应该有更灵活的选项。
  • 我需要抓取一个 javscript 网站。有什么我可以使用的建议吗?
  • 在这种情况下没有更好的建议。

标签: python selenium proxy phantomjs


【解决方案1】:

我正在编译来自: How to correctly pass basic auth (every click) using Selenium and phantomjs webdriver 也: base64.b64encode error

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import base64

service_args = [
    '--proxy=http://fr.proxymesh.com:31280',
    '--proxy-type=http',
]

authentication_token = "Basic " + base64.b64encode(b'username:password')

capa = DesiredCapabilities.PHANTOMJS
capa['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token
driver = webdriver.PhantomJS(desired_capabilities=capa, service_args=service_args)

driver.get("http://...")

【讨论】:

  • 难以置信。被困在这几个星期了。谢谢!
  • 你收到赏金了吗?我以前从未使用过它,所以我不知道我是否需要做其他事情才能给你赏金。
  • 我认为某处有一个赏金奖励按钮(但它应该在赏金到期时奖励它,因为你接受了答案):stackoverflow.com/help/bounty
【解决方案2】:

DesiredCapabilities 的解决方案对我不起作用。 我最终得到了以下解决方案:

from selenium import webdriver  

driver = webdriver.PhantomJS(executable_path=config.PHANTOMJS_PATH, 
service_args=['--ignore-ssl-errors=true',
    '--ssl-protocol=any',
    '--proxy={}'.format(self.proxy),
    '--proxy-type=http',
    '--proxy-auth={}:{}'.format(self.proxy_username, self.proxy_password)])

【讨论】:

    【解决方案3】:

    以上方法都不适合我,我正在使用带有 selenium phantomJs python 的 ProxyMeshproxies。并且以下参数对我有用,因为它解决了错误proxy authentication failed

    service_args=['--proxy=http://username:password@host:port',
                  '--proxy-type=http',
                  '--proxy-auth=username:password']
    
    driver = webdriver.PhantomJS(service_args=service_args) 
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2016-08-05
      • 2019-01-01
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多