python获取cookie登录(Python3之利用Cookie模拟登录)
python获取cookie登录(Python3之利用Cookie模拟登录)Connection: keep-aliveHost: demo.bxcker.com3. 进入客户管理模块,显示客户列表。4.通过抓包工具抓取客户列表,得到登录后的Cookie信息。GET http://demo.bxcker.com/customer/index.shtml HTTP/1.1
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登录成功,然后跳转到客户管理模块,获取的客户信息。