python 爬虫 selenium_Python爬虫获取cookie:利用selenium

下载、安装selenium

下载地址:https://pypi.python.org/pypi/selenium

目前的版本是:3.0.0b2

支持:Firefox, Chrome, Internet Explorer, PhantomJS

jar包的下载:selenium-3.0.0b2.tar.gz

解压selenium-3.0.0b2.tar.gz,进入selenium-3.0.0b2目录,执行python setup.py install直接安装.

下载浏览器Driver Server

解压,并将解压目录添加到环境变量中。

注意1:在windows 64位系统也要使用IEDriverServer Win32版本的,否则可能会出现错误In particular, be sure you are not attempting to use a 64-bit IEDriverServer.exe against IE 10 or 11, even on 64-bit Windows.

注意2:geckodriver不能在cygwin中使用。IEDriverServer在windows和cygwin环境中都可以使用。

注意3: 关于geckodriver的说明: 早期名字叫wires,如果提示wires找不到,可以将geckodriver重命名为wires。

示例1:通过IE获取cookie

在python命令行中执行:

>>>from selenium import webdriver

>>>ie = webdriver.Ie()

>>>ie.get("http://www.cnvd.org.cn")

>>>ie.get_cookies()

[{'value': 'CA2DD4EBD61BECAC3C19546F4AA52BD0', 'httpOnly': False, 'name': 'JSESSIONID', 'secure': False}, {'secure': False, 'httpOnly': False, 'expiry': 1470457299, 'value': '1470453699.622|0|sWEFsKmkH%2FTLajrFqRkRWKSdTeY%3D', 'domain': 'www.cnvd.org.cn', 'path': '/', 'name': '__jsl_clearance'}, {'value': '14dbf9b7ec3482ba76b140b2e2a8ae14', 'httpOnly': True, 'name': '__jsluid', 'secure': False}, {'secure': False, 'httpOnly': False, 'expiry': 1659704113, 'value': '1470401713484', 'domain': 'www.cnvd.org.cn', 'path': '/', 'name': 'bdshare_firstime'}]

如果你的IE浏览器页面的放大率不是100%,则会有如下错误:

selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Browser zoom level was set to 130%. It should be set to 100%

解决方法:点击浏览器右下角的“更改缩放级别”,改为100%

示例2:通过Firefox获取cookie

>>>from selenium import webdriver

>>>firefox = webdriver.Firefox()

>>>firefox.get("http://www.cnvd.org.cn")

>>>firefox.get_cookies()

[{'name': '__jsluid', 'expiry': None, 'httpOnly': True, 'secure': False, 'path': '/', 'domain': 'www.cnvd.org.cn', 'value': '6227ceeae8067fc9f47f832093b92067'}, {'name': '__jsl_clearance', 'expiry': None, 'httpOnly': False, 'secure': False, 'path': '/', 'domain': 'www.cnvd.org.cn', 'value': '1470453972.745|0|5w9OUDO2vOYMvowWI%2BF3xGBQlf0%3D'}, {'name': 'JSESSIONID', 'expiry': None, 'httpOnly': False, 'secure': False, 'path': '/', 'domain': 'www.cnvd.org.cn', 'value': '91EC775B4CF2D948FC74E126D9E17013'}, {'name': 'bdshare_firstime', 'expiry': None, 'httpOnly': False, 'secure': False, 'path': '/', 'domain': 'www.cnvd.org.cn', 'value': '1470453972212'}]

在cygwin中执行firefox = webdriver.Firefox(),会有错误selenium.common.exceptions.WebDriverException: Message: entity not found,在windows环境执行中不会有问题。

示例3:python脚本

get_cookie.py

import time

from selenium import webdriver

from selenium.common.exceptions import WebDriverException

def GetCookie():

url = "http://www.cnvd.org.cn/flaw/list.htm"

cookies = []

try:

print('open IE browser')

ie = webdriver.Ie()

print('visit cnvd website')

ie.get(url)

timesleep = 8 #需要延时,来获取完整的cookies

print('sleep {} seconds'.format(timesleep))

time.sleep(timesleep) # important to get full cookies

except WebDriverException as wde:

print(wde)

if ie != None:

ie.quit()

else:

print('get cookies...')

cookies = ie.get_cookies()

ie.quit()

if cookies == '' or type(cookies) != list or cookies.__len__() == 0:

print('cookie is not found')

else:

print('cookies: {}, size: {}'.format(cookies, cookies.__len__()))

GetCookie()

输出Log:

$ python3 get_cookie.py

open IE browser

visit cnvd website

sleep 8 seconds

get cookies...

cookies: [{'name': 'JSESSIONID', 'value': '288C44E9485D45D8CD6DCF5ECD45FE48', 'httpOnly': False, 'secure': False}, {'httpOnly': False, 'path': '/', 'name': '__jsl_clearance', 'domain': 'www.cnvd.org.cn', 'expiry': 1472317063, 'value': '1472313463.29|0|qhvo%2BKl%2BfNxrWIU82bwTrL%2BxISE%3D', 'secure': False}, {'name': '__jsluid', 'value': 'c44ca6d63264ac8b08e969cfb0390c3b', 'httpOnly': True, 'secure': False}, {'httpOnly': False, 'path': '/', 'name': 'bdshare_firstime', 'domain': 'www.cnvd.org.cn', 'expiry': 1661615471, 'value': '1472313071070', 'secure': False}], size: 4

可能出现的问题:

问题1:selenium.common.exceptions.NoSuchWindowException: Message: Unable to get browser

问题2:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

解决方法:Internet选项->安全->“Internet”,“本地Intranet”,“受信任的站点”,“受限制的站点”的“启用保护模式”需要设置成一样的。都设置成选中状态,可以解决。或者试试都设置成非选中状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值