Python 在 phantomjs + selenium 添加代理

概述:
网上有很多文章讲phantomjs+selenium如何添加代理IP的文章,但是亲自试了多个,均不可用。因为我用的是经过认证的代理,网上好多只提供了没有认证的代理IP的办法。而且还有说需要修改phantomjs或selenium源码的,我认为这种办法的可移植性太差。弄了两天多,还是没有办法,后来把问题发到了论坛里,有人给了下边的链接,在linux下只打印出来html空架子,但是看楼主是解决了的,于是认为是哪里出错了,后来又各种百度,偶然看到说Windows下没问题,linux下打印为空,于是我把代码放到Windows下执行,果然可以。
参考文档:
示例代码:
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://...")
问题:
在linux下只打印了一个空架子,在Windows下正常使用
解决办法:
linux下原phantomjs 版本为1.9.7,升级至2.1.1 后OK
附:phantomjs安装办法:
phantomjs直接下载安装包及可用,
1.wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2  
2.tar -xvf phantomjs-2.1.1-linux-x86_64.tar.bz2   
3.sudo cp /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/bin/
将phantomjs包拷入/usr/local/src/下,并将可执行文件 拷入/usr/bin/下
4.phantomjs -v 执行该命令能查看到版本号,即可。在代码中也不必指定executable_path

如有疑问,可关注作者公众号【学术派糊糊】留言


猜你喜欢

转载自blog.csdn.net/aysa_bear/article/details/80436711