cookies组成结构
cookie ={u'domain': u'.cnblogs.com', u'name': u'.CNBlogsCookie', u'value': u'xxxx', u'expiry': 1554959887, u'path': u'/', u'httpOnly': True, u'secure': False } name:cookie的名称 value:cookie对应的值,动态生成的 domain(多米):服务器域名 expiry(伊克斯百瑞):Cookie有效终止日期 path:Path属性定义了Web服务器上哪些路径下的页面可获取服务器设置的Cookie httpOnly:防脚本攻击 secure(c q):在Cookie中标记该变量,表明只有当浏览器和Web Server之间的通信协议为加密认证协议时, 浏览器才向服务器提交相应的Cookie。当前这种协议只有一种,即为HTTPS。
正则提取body内容
import requests import re url = "http://47.104.190.48:8000/login/" r = requests.get(url) print(r.cookies) # 返回的cookies # print(r.text) # re 正则表达式,知道前面和后面,取中间值,取出来是个list token = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r.text) print(token[0]) body = { "csrfmiddlewaretoken": token[0], "username": "test111", "password": "123456" } r2 = requests.post(url, data=body, cookies=r.cookies) print(r2.text) # 判断是否登录成功 assert "登录成功!" in r2.text
重定向302
import requests url = "http://47.104.190.48:8000/change/" r = requests.get(url) #r就是一个Response对象 print(r.status_code) # 最后一个状态码 print(r.url) print(r.headers) all = r.history # 是个list,里面放的是Response对象302,history不包括最后一次返回的状 态码 print(all[0].status_code) print(all[0].headers['Location']) print(all[0].url) # for i in all: # print(i) # Response [302] 对象 从定向302是没有body的
取token值
import requests import re s = requests.session() # 代码里面的浏览器 print(s.headers) print(s.cookies) url = "http://47.104.190.48:8000/register/" r1 = s.get(url) print(s.cookies) token = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r1.text) print(token[0]) body = { "csrfmiddlewaretoken": token[0], "username": "t1223", "password": "111", "email": "111", } r2 = s.post(url, data=body) print(r2.text)
post
import requests import re s = requests.session() # 代码里面的浏览器 print(s.headers) print(s.cookies) url = "http://47.104.190.48:8000/register/" r1 = s.get(url) print(s.cookies) token = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r1.text) print(token[0]) body = { "csrfmiddlewaretoken": token[0], "username": "t1223", "password": "111", "email": "111", } r2 = s.post(url, data=body) print(r2.text)
post
import requests import re s = requests.session() url = "http://47.104.190.48:8000/login/" r = s.get(url) # re 知道前面和后面,取中间值 token = re.findall('name="csrfmiddlewaretoken" value="(.+?)"', r.text) print(token[0]) body = { "csrfmiddlewaretoken": token[0], "username": "t1223", "password": "111" } r2 = s.post(url, data=body) print(dict(s.cookies)) #创建一个字典 # 判断是否登录成功 assert "登录成功!" in r2.text ''' {'csrftoken': 'FeFCoSpsogUcPSOcWywWLXnXAjQbZivbdEGL9dIFFB6ytVHoM3xJg2JaJzqkXN92',get请求时候给的 'sessionid': '3ssepjor6hcpknl4hdqe4ir1d1g4b0ek'} 服务端登录完成之后给的 ''' # 登陆之后的 url3 = "http://47.104.190.48:8000/change/" r3 = s.get(url3)
post
import requests s = requests.session() # 添加cookies cooks = { ".CNBlogsCookie": "A263989267CD17B3EBED28C95945F177C2FE879E34D9DDB1A70F21F79A6546AD09AB4E621EABDAE9AB9FD66AC41FB2F4F77C7D9A9C0F978704D28478B1C67D49CE37FAC26CE4CDEDD5A249F47C3EA99793FE2143", ".Cnblogs.AspNetCore.Cookies": "CfDJ8JcopKY7yQlPr3eegllP76Pd3IDlP1FzpXui7yd2E_cRaTRNbJVf7XaWcIYC6bwmMUJE9vs9RbRrHnMc1zjTENv9A4LTYxHW_MVPg_gnoknmqUFZQu02udPSa41QJUbaYxA6cXLNygQGThIlbXB6RWimByk1-f0y12fwXz25iohSeyUIxNzCOpA4xbMMIAUQbI31glDeq-hKOlcm1BbyGMtr_SZmPMVHrDAWHBvbZRmd0srPRGAzFrpPh4-3_BXs9DnNpLAW7BC--zm01jIVFJ_f_nhASgVPahkuiXSSQBm4WTposjhlCkuiHgkDPrD36A" } # 工具匹配不到,自己写,添加cookies键值对 c = requests.cookies.RequestsCookieJar() c.set(".CNBlogsCookie", cooks[".CNBlogsCookie"]) # 登陆有效的cookies c.set(".Cnblogs.AspNetCore.Cookies", cooks[".Cnblogs.AspNetCore.Cookies"]) s.cookies.update(c) #更新cookies print(s.cookies) url = "https://i.cnblogs.com/EditPosts.aspx?opt=1" r = s.get(url) print(r.text)
post
import requests s = requests.session() print(s.headers) token = "1231231231231" h = { "token": token } s.headers.update(h) print(s.headers)
11111