Python3之利用Cookie模拟登录
Posted 听海8
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3之利用Cookie模拟登录相关的知识,希望对你有一定的参考价值。
Python3之利用Cookie模拟登录
利用Cookie模拟登录步骤:
1. 在浏览器输入http://demo.bxcker.com,输入用户名和密码登录。
2.登录成功点“客户管理”模块。
3. 进入客户管理模块,显示客户列表。
4.通过抓包工具抓取客户列表,得到登录后的Cookie信息。
GET http://demo.bxcker.com/customer/index.shtml HTTP/1.1
Host: demo.bxcker.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: */*
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36
Referer: http://demo.bxcker.com/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: IEX-JSESSIONID=WFPlPUVbbbkQ76XPCr9tXRB4voK0eESo; Hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; Hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; SPRING_SECURITY_REMEMBER_ME_COOKIE=UE9kSDFGMnVsS291S2Z2V1k5c1daQT09OjZxWFVDUUhoVmR2UTdsWnRFRnlZZ0E9PQ
5.利用sublime text工具处理抓到的http请求头信息替换成字典
选择内容替换的方式是用正则表达式
内容填写:^(.*):\\s(.*)$ (需要把header文件内容处理成字典,中间有个空格,所以加个\\s)
替换为填写:"\\1":"\\2",
点全换,处理完成之后变成如下:
"Host":"demo.bxcker.com",
"Connection":"keep-alive",
"Pragma":"no-cache",
"Cache-Control":"no-cache",
"Accept":"*/*",
"X-Requested-With":"XMLHttpRequest",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
"Referer":"http://demo.bxcker.com/",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.9",
"Cookie":"IEX-JSESSIONID=WFPlPUVbbbkQ76XPCr9tXRB4voK0eESo; Hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; Hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; SPRING_SECURITY_REMEMBER_ME_COOKIE=UE9kSDFGMnVsS291S2Z2V1k5c1daQT09OjZxWFVDUUhoVmR2UTdsWnRFRnlZZ0E9PQ",
6. 处理完成之后直接拷贝到Python定义的头信息变量headers={}里
7. 整理headers 信息(去掉一些没用的,保留一些重要的)
"Connection":"keep-alive",
"X-Requested-With":"XMLHttpRequest",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
"Referer":"http://demo.bxcker.com/",
"Cookie":"IEX-JSESSIONID=WFPlPUVbbbkQ76XPCr9tXRB4voK0eESo; Hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; Hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; SPRING_SECURITY_REMEMBER_ME_COOKIE=UE9kSDFGMnVsS291S2Z2V1k5c1daQT09OjZxWFVDUUhoVmR2UTdsWnRFRnlZZ0E9PQ",
8. 编写完整的程序。
from urllib import parse
import urllib.request
url ="http://demo.bxcker.com/customer/index.shtml"
headers={
"Host":"demo.bxcker.com",
"Connection":"keep-alive",
"Pragma":"no-cache",
"Cache-Control":"no-cache",
"Accept":"*/*",
"X-Requested-With":"XMLHttpRequest",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
"Referer":"http://demo.bxcker.com/",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.9",
"Cookie":"IEX-JSESSIONID=WFPlPUVbbbkQ76XPCr9tXRB4voK0eESo; Hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; Hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; SPRING_SECURITY_REMEMBER_ME_COOKIE=UE9kSDFGMnVsS291S2Z2V1k5c1daQT09OjZxWFVDUUhoVmR2UTdsWnRFRnlZZ0E9PQ",
}
# url 作为Request()方法的参数,构造并返回一个Request对象
request = urllib.request.Request(url)
# Request对象作为urlopen()方法的参数,发送给服务器并接收响应
response = urllib.request.urlopen(request)
html = response.read().decode(\'utf-8\')
print(html)
运行结果:
可以看到客户列表里的相关信息,证明是通过Cookie登录成功,然后跳转到客户管理模块,获取的客户信息。
---------------------------------------
个人今日头条账号: 听海8 (上面上传了很多相关学习的视频以及我书里的文章,大家想看视频,可以关注我的今日头条)
以上是关于Python3之利用Cookie模拟登录的主要内容,如果未能解决你的问题,请参考以下文章
urllib中的保存cookie使用,运用cookiejar来模拟登录人人网