标签:
保存当前cookie到本地
import urllib.request as ur import http.cookiejar as hc url=‘http://www.xxxx.com/admin/‘ filename=‘cookie.txt‘ cookie=hc.MozillaCookieJar(filename) handler=ur.HTTPCookieProcessor(cookie) opener=ur.build_opener(handler) req=ur.Request(url) res=opener.open(req) cookie.save(ignore_discard=True, ignore_expires=True)
加载本地cookie登录网站(先手工登录网站,通过F12获取cookie信息,修改本地cookie.txt,就可以使用下面代码登录网站了)
import urllib.request as ur import http.cookiejar as hc url=‘http://www.xxxx.com/admin/‘ cookie=hc.MozillaCookieJar() cookie.load(‘cookie.txt‘,ignore_discard=True, ignore_expires=True) handler=ur.HTTPCookieProcessor(cookie) opener=ur.build_opener(handler) req=ur.Request(url) res=opener.open(req) print(res.read().decode(‘utf8‘))
关于cookie.save和cookie.load的后面两个参数官网说明
ignore_discard: save even cookies set to be discarded.
ignore_expires: save even cookies that have expiredThe file is overwritten if it already exists
已经测试过,参数必须加上,不然运行错误
标签:
原文地址:http://www.cnblogs.com/fj0716/p/5159376.html