cookie登陆简书
'''
.::::. * # _oo0oo_ #
.::::::::. * # o8888888o #
::::::::::: * # 88" . "88 #
..:::::::::::' * # (| -_- |) #
'::::::::::::' * # 0\ = /0 #
.:::::::::: * # ___/`---'\___ #
'::::::::::::::.. * # .' \\| |# '. #
..::::::::::::. * # / \\||| : |||# \ #
``:::::::::::::::: * # / _||||| -:- |||||- \ #
::::``:::::::::' .:::. * # | | \\\ - #/ | | #
::::' ':::::' .::::::::. * # | \_| ''\---/'' |_/ | #
.::::' :::: .:::::::'::::. * # \ .-\__ '-' ___/-. / #
.:::' ::::: .:::::::::' ':::::. * # ___'. .' /--.--\ `. .'___ #
.::' :::::.:::::::::' ':::::. * # ."" '< `.___\_<|>_/___.' >' "". #
.::' ::::::::::::::' ``::::. * # | | : `- \`.;`\ _ /`;.`/ - ` : | | #
...::: ::::::::::::' ``::. * # \ \ `_. \_ __\ /__ _/ .-` / / #
```` ':. ':::::::::' ::::.. * # =====`-.____`.___ \_____/___.-`___.-'===== #
'.:::::' ':'````.. * # `=---=' #
女神保佑 永无BUG * # 佛祖保佑 永无BUG #
'''
#获取一个有登陆信息的Co'okie模拟登陆
from urllib import request
import chardet
#1.构建一个已经成功登陆的用户的headers信息
headers = {
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0",
#这个cookie是保存了密码无需重复登陆的用户Cookie,这个Cookie里记录了用户名,密码
"cookie":"..."
}
#2.通过cookie里的报头信息(主要是cookie信息),构建Request对象
req = request.Request("https://www.jianshu.com/u/f653a9561181",headers = headers)
#3.直接访问,服务器会根据headers报头信息,判断这是一个已经登陆过的用户
response = request.urlopen(req)
#4.打印响应内容
html = response.read()
charset = chardet.detect(html)['encoding']
print(charset)
print(html.decode(charset))