要点:1.selenium的cookies是多个字典组成的list,而且有很多键值
2.selenium 想要添加cookies,得先登录对应域名的网址,再添加,一旦cookies里有不同域名(domain)就会报错!
3.requests的cookies只需name跟value即可
最近研究用selenuim 登录纯JS加载的网站获取信息,到后面想用cookies免登录时,发现selenium跟requests 使用cookies的一些差别
用selenuim登录网站后,driver.get_cookies()就会得到此时的cookies,注意,这是多个dict组成的list登录企名片(http://auth.qimingpian.com/finos.html)后cookies的内容如下:
cookies=\ [ {'domain': '.vip.qimingpian.com', 'path': '/', 'httpOnly': False, 'name': 'userinfo', 'expiry': None, 'secure': False, 'value': '{%22nickname%22:%22%E5%81%A5%EB%88%88_%EB%88%88%22%2C%22headimgurl%22:%22http://img1.qimingpian.com/wximg/171351ea5349c8c3723c808b048ba818.jpg%22%2C%22coin%22:%2250%22%2C%22applySubmit%22:%220%22%2C%22team_flag%22:%220%22%2C%22team_uuid%22:%22%22%2C%22vip_out_date%22:%22%22%2C%22usernum%22:%22864636697%22%2C%22team_enterprise%22:%220%22%2C%22enterprise_coin%22:%220%22%2C%22is_admin%22:%220%22%2C%22is_manager%22:%220%22%2C%22first_shenqing%22:%220%22%2C%22phone%22:%2213630499459%22%2C%22apply_phone%22:%2213630499459%22%2C%22scope%22:%22qmp%22%2C%22apply_state%22:3%2C%22liyou%22:%22%22%2C%22is_certify%22:0%2C%22ip%22:%22183.2.90.153%22%2C%22person_role%22:%22%22%2C%22claim_type%22:0%2C%22expireinfo%22:false%2C%22inneruser%22:false}'}, {'domain': '.qimingpian.com', 'path': '/', 'httpOnly': False, 'name': 'time_token', 'expiry': 1529368822, 'secure': False, 'value': '1528504828414'}, {'domain': '.vip.qimingpian.com', 'path': '/', 'httpOnly': False, 'name': 'Hm_lvt_1e712c5331439bcf163b46f3d208f00b', 'expiry': 1560040822, 'secure': False, 'value': '1528191935,1528192457,1528504823'}, {'domain': '.vip.qimingpian.com', 'path': '/', 'httpOnly': False, 'name': 'Hm_lpvt_1e712c5331439bcf163b46f3d208f00b', 'expiry': None, 'secure': False, 'value': '1528504823'}, {'domain': 'vip.qimingpian.com', 'path': '/', 'httpOnly': False, 'name': '102921|0|0|9|1|1|', 'expiry': 1528504833, 'secure': False, 'value': '102921%7C0%7C0%7C9%7C1%7C1%7C'}, {'domain': '.qimingpian.com', 'path': '/', 'httpOnly': False, 'name': 'unionid', 'expiry': 1529368823, 'secure': False, 'value': 'HYSyhBeZG1C9ydWJCU9/LhfTok2z/lioTdcZ4Td+LyS3oTbOlCL1akRJTwSrhAldeJWqqIs6kiQsM8IbOYgM5A=='}, {'domain': 'vip.qimingpian.com', 'path': '/', 'httpOnly': False, 'name': 'userinfo', 'expiry': None, 'secure': False, 'value': '{%22nickname%22:%22%E5%81%A5%EB%88%88_%EB%88%88%22%2C%22headimgurl%22:%22http://img1.qimingpian.com/wximg/171351ea5349c8c3723c808b048ba818.jpg%22%2C%22coin%22:%2250%22%2C%22applySubmit%22:%220%22%2C%22team_flag%22:%220%22%2C%22team_uuid%22:%22%22%2C%22vip_out_date%22:%22%22%2C%22usernum%22:%22864636697%22%2C%22team_enterprise%22:%220%22%2C%22enterprise_coin%22:%220%22%2C%22is_admin%22:%220%22%2C%22is_manager%22:%220%22%2C%22first_shenqing%22:%220%22%2C%22phone%22:%2213630499459%22%2C%22apply_phone%22:%2213630499459%22%2C%22scope%22:%22qmp%22%2C%22apply_state%22:3%2C%22liyou%22:%22%22%2C%22is_certify%22:0%2C%22ip%22:%22113.93.105.10%22%2C%22person_role%22:%22%22%2C%22claim_type%22:0%2C%22expireinfo%22:false%2C%22inneruser%22:false%2C%22apply_pro_state%22:3}'}, ]#可以看到,每个字典有多个键,它们的含义是:
selenium.common.exceptions.InvalidCookieDomainException: Message: Cookies may only be set for the current domain (auth.qimingpian.com)
请看看domain:4个带vip,2个是不带vip的,域名不同,所以报错,琢磨了下,我改为这样,
driver.get('http://auth.qimingpian.com/finos.html') for i in cookies: if i['domain'][1:] in driver.current_url: driver.add_cookie(i) driver.get('http://vip.qimingpian.com/#/finos/investment/ishijian') for i in cookies: if i['domain'][1:] in driver.current_url: driver.add_cookie(i) driver.refresh()
ok!此时登录成功,特别说明下,selenium添加cookies时,得先登录网址才能添加cookies的
cookies1={} for cookie in cookies: cookies1[cookie['name']]=cookie['value']
cookies1={ 'time_token': '1528504828414', 'Hm_lpvt_1e712c5331439bcf163b46f3d208f00b': '1528504823', 'Hm_lvt_1e712c5331439bcf163b46f3d208f00b': '1528191935,1528192457,1528504823', 'unionid': 'HYSyhBeZG1C9ydWJCU9/LhfTok2z/lioTdcZ4Td+LyS3oTbOlCL1akRJTwSrhAldeJWqqIs6kiQsM8IbOYgM5A==', 'userinfo': '{%22nickname%22:%22%E5%81%A5%EB%88%88_%EB%88%88%22%2C%22headimgurl%22:%22http://img1.qimingpian.com/wximg/171351ea5349c8c3723c808b048ba818.jpg%22%2C%22coin%22:%2250%22%2C%22applySubmit%22:%220%22%2C%22team_flag%22:%220%22%2C%22team_uuid%22:%22%22%2C%22vip_out_date%22:%22%22%2C%22usernum%22:%22864636697%22%2C%22team_enterprise%22:%220%22%2C%22enterprise_coin%22:%220%22%2C%22is_admin%22:%220%22%2C%22is_manager%22:%220%22%2C%22first_shenqing%22:%220%22%2C%22phone%22:%2213630499459%22%2C%22apply_phone%22:%2213630499459%22%2C%22scope%22:%22qmp%22%2C%22apply_state%22:3%2C%22liyou%22:%22%22%2C%22is_certify%22:0%2C%22ip%22:%22113.93.105.10%22%2C%22person_role%22:%22%22%2C%22claim_type%22:0%2C%22expireinfo%22:false%2C%22inneruser%22:false%2C%22apply_pro_state%22:3}', '102921|0|0|9|1|1|': '102921%7C0%7C0%7C9%7C1%7C1%7C' }此时requests.get(url, cookies =cookies1, headers =headers) 即可
不过,如果你想反过来,用request的cookies在selenium上登陆,那是不行的!因为domain得指定!!
=========================================================================
在研究selenium登录百度时,发现很蛋疼的事,原来百度的cookies是不通用的!
百度首页:https://www.baidu.com/
百度贴吧:https://tieba.baidu.com
百度网盘:https://pan.baidu.com/
如果你用selenium登录了百度首页,然后把cookies保存下来,用此cookies是不能直接登录贴吧跟网盘的!不过网盘跟贴吧的倒可以直接登录首页。
原因出自哪里呢?原来还是domain! 贴吧是'.tieba.baidu.com' 网盘是'.pan.baidu.com'
不同的,所以想要同时能登录,每个域名都得保持下来