说到添加cookie绕过登陆这部分,我个人就很喜欢用firefox,为什么呢,因为firefox添加cookie真的很方便。添加的时候三行代码就好了。
self.driver.get(url)
self.driver.add_cookie({'name': 'Cookie', 'value': 'UM_distinctid=169897e2570233-0755822f71bc2f-5e1d3712-13c680-169897e25711c1; uuid="w:8821155e45214890a45e0f084fa92453"; __utma=24953151.1291006960.1552801511.1552801511.1552801511.1; __utmz=24953151.1552801511.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.2.1291006960.1552801511; _ba=BA0.2-20190401-5110e-0vlRpicFZmTkR4eXxxZs; _ga=GA1.3.1291006960.1552801511; passport_auth_status=ad03d64f5cb3c466f932fe366935db5f; toutiao_sso_user=5ac026fbfdfcfc1ee00447708619ad87; login_flag=345e04c80b25d51bf7aa657955113c66; sessionid=4019df05b9de3b41206466aa405ffd95; sid_tt=4019df05b9de3b41206466aa405ffd95; sso_uid_tt=1badfe650a234469b19699fe2f64d437; uid_tt=197d4e386527179804a26cd94b52497f; _gid=GA1.2.1095874391.1554192332; tt_webid=75485685355; _gid=GA1.3.1095874391.1554192332; __tea_sdk__ssid=499215b5-a3e7-4cd3-add2-7456078da2ab; tt_webid=6675469253791319559; __tea_sdk__user_unique_id=6675469253791319559; sid_guard=4019df05b9de3b41206466aa405ffd95%7C1554271053%7C2427782%7CWed%2C+01-May-2019+08%3A20%3A35+GMT; s_v_web_id=d11d589c65bcb5d0b2bcb0966148e79d; ptcn_no=c267e6e5cc73d3b7362b5c30f50bdb89'})
self.driver.refresh()
value的值直接在开发者工具cookie后面那段就是,非常方便。
现在说说chrome是怎么麻烦的,相信大家找了很多博客,都说什么name阿,value,domain,path,expires,还要一一对应,下面我贴一下有效代码
先手动登陆,为了就是get_cookies,把它写成一个json。这里保存为cookies.json
然后再一个for循环把cookie导入到网站,不多点尝试还真试不出来。笔者为了解决chrome上添加cookie,真的整整花了一个下午才解决。希望对大家有帮助。
附上代码
cookies = brower.get_cookies() time.sleep(2) # with open('cookie.txt', 'w')as f: # f.write(str(cookies)) print(cookies) jsonCookies = json.dumps(cookies) # 登录完成后,将cookie保存到本地文件 with open('cookies.json', 'w') as f: f.write(jsonCookies)
——————————————————————————————
with open('cookies.json', 'r') as f: listCookies = json.loads(f.read()) for cookie in listCookies: brower.add_cookie({ 'domain': '.huke88.com', # 此处xxx.com前,需要带点 'name': cookie['name'], 'value': cookie['value'], 'path': '/', 'expires': None }) # 再次访问页面,便可实现免登陆访问 brower.refresh() time.sleep(1000)