使用cookie登陆知乎

只是想说明一个问题,Cookie可以维持登录状态,有些网页当中,访问之后的cookie里面带有登陆账号,和登陆密码,这样可以使用cookie直接访问网页,如知乎,首先登录知乎,将Headers中的Cookie内容复制下来

使用cookie登陆知乎_第1张图片

这个需要替换成你自己的Cookie,将其设置到Headers里面,然后发送请求,示例如下:

import requests

headers = {
    'Cookie': '_zap=e73c4291-bff5-4dca-a3e8-12345628bf14; q_c1=9efbde83f64144f1b4a8d090be370c69|1519296111000|1513584938000; aliyungf_tc=AQAAAGF+AF4PXw4AOmvpelfN68YHiUdr; d_c0="AABsIlRvMA2PTvbEewf7ePZWD0eTbaPrJ2w=|1519368709"; _xsrf=c4e0ac6d-e533-4635-b74e-b386c108c172; __utmc=51854390; __utmz=51854390.1519368755.1.1.utmcsr=zhihu.com|utmccn=(referral)|utmcmd=referral|utmcct=/; r_cap_id="MmEwNWQ4ZDdlOGI1NGQ5NThiMTgzMmE2Y2VmMzE5MWU=|1519372419|ce0c41d982f4920eee847fd61973547e5e3663f3"; cap_id="Y2Y4YzU1MWFlODJiNDU2OWI1MDFmOGFjMDRkYmIyYjg=|1519372419|645a3ec27138c8c057565a0ff4331e0892e326ff"; l_cap_id="NGMwNDU2YzE2MzUyNGJjMWI1YWJiNDFmYTRmZGMzZTI=|1519372420|b81871dcd2710733144103dc6067c280fd4d8959"; __utma=51854390.2043311141.1519368755.1519368755.1519372412.2; __utmv=51854390.000--|2=registration_date=20170601=1^3=entry_date=20180223=1; capsion_ticket="2|1:0|10:1519375103|14:capsion_ticket|44:NjMyYjYyMjA0OGExNGVhOGIzM2M3NTYyOTk5NzQ5NWY=|ef6ee520d3cc196bc1d6ba3bb08906808257411eb9241201d7cb66ac5119b6b7"; z_c0="2|1:0|10:1519375116|4:z_c0|92:Mi4xMlZzWUJRQUFBQUFBQUd3aVZHOHdEU1lBQUFCZ0FsVk5EQ0Y5V3dDWXZXZ0ZoUFJFMjAtMjNUYmhNOExGNUYyazln|7e73fcc85eb02dff603ef615448dbdf32d73f7a80d93347e803445c53437bce7"',
    'Host': 'www.zhihu.com',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
}
r = requests.get('https://www.zhihu.com', headers=headers)
print(r.text)

你可能感兴趣的:(爬虫)