1、下载PhantoJS,到PhantomJS官网‘http://phantomjs.org/download.html’选择Windows版本下载即可
2、解压后进入解压文件bin目录,双击phantomjs.exe即可打开phantomjs命令行窗口输入1+23说明phantomjs正常
3、拷贝文件夹到C盘根目录,命名为phantomjs,这里主要是为了方便操作,也可以将phantomjs.exe路径放到系统环境变量的path中。
4、安装python包selenium,这个是用来操纵phantomjs浏览器的在电脑运行输入‘cmd’进入命令行窗口。输入pip install selenium
5、打开Python开发工具IDLE,新建‘phantom.py’文件,写代码如下;from selenium import webdriverdriver = webdriver.PhantomJS(executable_path=r'c:/phantomjs/bin/phantomjs.exe')
6、F5运行程序,弹出phantomjs.exe窗口,说明正常被调用了,python shell提示的warning,意思是selenium建议使用chrome或firefox的驱动而不是phantomjs,这个不用理会
7、用selenium操作phantomjs获取百度页面,代码如下;from sele艘绒庳焰nium import webdriverdriver = webdriver.PhantomJS(executable_path=r'c:/phantomjs/bin/phantomjs.exe')driver.get('http://www.baidu.com')print (driver.page_source)
8、F5运行程序,phantomjs浏览器正常,获取到百度首页