赞
踩
- def save_cookie(browser):
- '''
- 说明:pickle库来序列化数据(把格式数据存入文件和加载到内存)
- '''
- cookies = browser.get_cookies()
- # 方法一
- # with open('taobao_cookie.pickle','wb') as f:
- # pickle.dump(cookies, f)
-
- # 方法二
- pickle.dump(cookies, open('taobao_cookies.pkl','wb'))
- def get_cookie(browser):
-
- # 清除浏览器打开已有的cookies
- browser.delete_all_cookies()
- # 方法一
- # with open('taobao_cookie','rb') as f:
- # cookies = pickle.load(f)
- # for cookie in cookies:
- # if 'expiry' in cookie:
- # del cookie['expiry']
- #
- # browser.add_cookie(cookie)
-
-
- # 方法二
- cookies = pickle.load(open("taobao_cookies.pkl", "rb"))
- for cookie in cookies:
- if isinstance(cookie.get('expiry'), float):
- cookie['expiry'] = int(cookie['expiry'])
-
- browser.add_cookie(cookie)
open 中的 taobao_cookies.pkl 是保存 cookie 的文件,该文件的路径在当前 python 文件的平级目录下。读 cookie 时若出现异常,请参考《Python selenium中读取cookie报错:ERROR Message: invalid argument: invalid 'expiry'》
——————————
如有不足请留言指正
相互学习,共同进步
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。