您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python proxy-auth中的phantomjs selenium无法正常工作

5b51 2022/1/14 8:23:06 python 字数 2321 阅读 570 来源 www.jb51.cc/python

我正在尝试使用selenium phantomjs设置webscraping的代理.我正在使用 python. 我在很多地方都看到phantomjs中存在一个bug,因为proxy-auth不起作用. from selenium.webdriver.common.proxy import * from selenium import webdriver from selenium.webdrive

概述

我在很多地方都看到phantomjs中存在一个bug,因为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’: ‘Basic ‘+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',]

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

但它不起作用.

有关如何使其工作的任何建议?

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

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

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://...")

总结

以上是编程之家为你收集整理的python proxy-auth中的phantomjs selenium无法正常工作全部内容,希望文章能够帮你解决python proxy-auth中的phantomjs selenium无法正常工作所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶