python使用selenium、PhantomJS获得网站cookie信息#windows

首先python安装selenium,命令行中输入

pip install selenium

在执行代码如下代码时出现错误

driver=webdriver.PhantomJS()

错误如下

selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH. 

解决方案:

问题没有下载PhantomJS,PhantomJS不需要像python模块那样安装,直接进入官网下载页面http://phantomjs.org/download.html下载相应版本安装即可。

装好解压后,把文件夹bin中的phantomjs.exe移到python安装路径文件夹中的Scripts中,至此,就已经在Win的环境下配置好了环境。

错误解决

 

获取cookie完整代码如下#将cookie写入cookie.txt文件中

from selenium import webdriver
import pickle
driver=webdriver.PhantomJS()
driver.get(url)  #此处url填写需要访问的地址
# 获得 cookie信息
cookie_list = driver.get_cookies()
print (cookie_list)
cookie_dict = {}
for cookie in cookie_list:
#写入文件
f = open('cookie.txt','wb+')
pickle.dump(cookie, f)
f.close()
   
cookie_dict[cookie['name']] = cookie['value']
 

 

有不懂的地方或者想要探讨问题可以qq联系:1163949417
posted @ 2017-10-16 02:08  Jacck  阅读(1033)  评论(0编辑  收藏  举报