首页 > 编程语言 > 详细

python3 爬虫

时间:2016-01-26 10:26:06      阅读:245      评论:0      收藏:0      [点我收藏+]

 

 

保存当前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

已经测试过,参数必须加上,不然运行错误

 

python3 爬虫

原文:http://www.cnblogs.com/fj0716/p/5159376.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!