一个通过筛选,一个提取cookie(有两种写法)
一、依赖接口
(1)第一种方法:requests.session
准备两个接口:一个登录接口,一个用户查询接口
登录接口:http://cms.duoceshi.cn/manage/loginJump.do
请求体:
userAccount:admin
loginPwd:123456
用户查询接口:http://cms.duoceshi.cn/manage/queryUserList.do
请求体:
startCreateDate:
endCreateDate:
searchValue:
page:1
import requests s=requests.Session() url="http://cms.duoceshi.cn/manage/loginJump.do" data={'userAccount':'admin','loginPwd':'123456'} headers={"Content-Type":"application/x-www-form-urlencoded"} dx=s.request("post",url=url,data=data,headers=headers,) print(dx.text) #{"code":"200","msg":"登录成功!","model":{}} 响应体 url2="http://cms.duoceshi.cn/manage/queryUserList.do" data2={'startCreateDate':'','endCreateDate':'','searchValue':'','page':1} headers2={"Content-Type":"application/x-www-form-urlencoded"} dx2=s.request("post",url=url2,data=data2,headers=headers2)
print(dx2.text)
(2)通过cookies保持会话
请求头有一个cookie值
将登录接口的cookie值提取(复制)出来,作为下一个接口的请求头
import requests class Cms(object): def __init__(self): pass def dl(self): url="http://cms.duoceshi.cn/manage/loginJump.do" data={'userAccount':'admin','loginPwd':'123456'} headers={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.request("post",url=url,data=data,headers=headers,) print(dx.text) #{"code":"200","msg":"登录成功!","model":{}} 响应体 # print(dx.cookies) c=str(dx.cookies) # 将cookie和转换成字符串 self.s=c.split(" ")[1] def yhgl(self): url2="http://cms.duoceshi.cn/manage/queryUserList.do" data2={'startCreateDate':'','endCreateDate':'','searchValue':'','page':1} headers2={"Content-Type":"application/x-www-form-urlencoded","Cookie":self.s} dx2=requests.request("post",url=url2,data=data2,headers=headers2) print(dx2.text) if __name__ == '__main__': d=Cms() d.dl() d.yhgl()
(2)通过提取登录接口的cooike ,作为一个返回值return
(1)
import requests class Cms(object): def __init__(self): pass def dl(self): url="http://cms.duoceshi.cn/manage/loginJump.do" data={'userAccount':'admin','loginPwd':'123456'} headers={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.request("post",url=url,data=data,headers=headers,) print(dx.text) #{"code":"200","msg":"登录成功!","model":{}} 响应体 # print(dx.cookies) c=str(dx.cookies) # 将cookie和转换成字符串 s=c.split(" ")[1] return s def yhgl(self): f=self.dl() url2="http://cms.duoceshi.cn/manage/queryUserList.do" data2={'startCreateDate':'','endCreateDate':'','searchValue':'','page':1} headers2={"Content-Type":"application/x-www-form-urlencoded","Cookie":f} dx2=requests.request("post",url=url2,data=data2,headers=headers2) print(dx2.text) if __name__ == '__main__': d=Cms() d.dl() d.yhgl()
关联接口:省份接口,关连接口
上一个接口的响应体,作为下一个接口的请求体
案例:
准备省份接口
城市接口:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince
城市接口:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity
入参:
byProvinceName:城市名称
案例1:管理接口单独调通
import requests s=requests.Session() class Gl(object): def __init__(self): pass def sf(self): url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince" headers = {"Content-Type": "application/x-www-form-urlencoded"} dx=s.request("get",url=url,headers=headers) print(dx.text) def cs(self): url2="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity" data2={"byProvinceName":"浙江"} headers2 = {"Content-Type": "application/x-www-form-urlencoded"} dx2=s.request("post",url=url2,data=data2,headers=headers2) print(dx2.text) if __name__ == '__main__': d=Gl() d.sf() d.cs()
管理接口:
将省份的响应体提取出来,用到re正则提取,
在存放在城市的请求体中
提取汉字:
<string>(.+?)</string> 内容匹配0次或1次或n次,
. 匹配任意字符 + 匹配1次或n次 ?是匹配0次或1次
(1)管理接口中 通过self.实例变量
import re import requests s=requests.Session() class Gl(object): def __init__(self): pass def sf(self): url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince" headers = {"Content-Type": "application/x-www-form-urlencoded"} dx=s.request("get",url=url,headers=headers) nr=dx.text self.tq=re.findall("<string>(.+?)</string>",nr)[3] print(self.tq) def cs(self): url2="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity" data2={"byProvinceName":self.tq} headers2 = {"Content-Type": "application/x-www-form-urlencoded"} dx2=s.request("post",url=url2,data=data2,headers=headers2) print(dx2.text) if __name__ == '__main__': d=Gl() d.sf() d.cs()
(2)通过return 来进行传递
import requests import re s=requests.Session() class Gl(object): def __init__(self): pass def sf(self): url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince" headers = {"Content-Type": "application/x-www-form-urlencoded"} dx=s.request("get",url=url,headers=headers) nr=dx.text tq=re.findall("<string>(.+?)</string>",nr)[3] return tq def cs(self): c=self.sf() url2="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity" data2={"byProvinceName":c} headers2 = {"Content-Type": "application/x-www-form-urlencoded"} dx2=s.request("post",url=url2,data=data2,headers=headers2) print(dx2.text) if __name__ == '__main__': d=Gl() d.sf()
d.cs()
重点是提取:所有的省份 tq=re.findall("<string>(.+?)</string>",nr)
(3)随机获取省份
import requests import re import random s=requests.Session() class Gl(object): def __init__(self): pass def sf(self): url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince" headers = {"Content-Type": "application/x-www-form-urlencoded"} dx=s.request("get",url=url,headers=headers) nr=dx.text tq=re.findall("<string>(.+?)</string>",nr) self.b=random.choice(tq) #调用随机模块 def cs(self): c=self.sf() url2="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity" data2={"byProvinceName":self.b} headers2 = {"Content-Type": "application/x-www-form-urlencoded"} dx2=s.request("post",url=url2,data=data2,headers=headers2) print(dx2.text) if __name__ == '__main__': d=Gl() d.sf() d.cs()
token案例:
案例:
网站:http://shop.duoceshi.com/login?redirect=%2Fdashboard
code接口:http://manage.duoceshi.com/auth/code
登录接口:http://manage.duoceshi.com/auth/login
登录接口参数:{"username":"admin","password":"e6WGOz+g/FuR646O7IF8JrlC6qH/anCI9/0UCsVDnUxN2aBdGKtRffNb1W7i87dRavZCNyP9yqvAcXLgdKtsRA==","code":"8888","uuid":"{{uuid}}"}
链接类型:Content-Type:application/json
搜索商品接口:http://manage.duoceshi.com/api/yxStoreCategory?page=0&size=10&sort=sort%2Cdesc
Authorization:
Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTY4MDE0OTgwNX0.fmlEdR5Xh1BF2al6crfPMjG8dd-M6TOKux3wni5ejvGFLce4Zk0tvKsCobj7WEaXvXI-N-21AIj2HH5LFlFMNw
一、如何提取tonken值token案例: 案例: 网站:http://shop.duoceshi.com/login?redirect=%2Fdashboard 验证码(固定):8888二、抓取接口code接口:http://manage.duoceshi.com/auth/code登录接口:http://manage.duoceshi.com/auth/login 登录接口参数:{"username":"admin","password":"e6WGOz+g/FuR646O7IF8JrlC6qH/anCI9/0UCsVDnUxN2aBdGKtRffNb1W7i87dRavZCNyP9yqvAcXLgdKtsRA==","code":"8888","uuid":"{{uuid}}"}入参类型:Content-Type:application/json搜索商品接口:http://manage.duoceshi.com/api/yxStoreCategory?page=0&size=10&sort=sort%2Cdesc Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTY4MDE0OTgwNX0.fmlEdR5Xh1BF2al6crfPMjG8dd-M6TOKux3wni5ejvGFLce4Zk0tvKsCobj7WEaXvXI-N-21AIj2HH5LFlFMNw三、实战
实践:抓取三个接口
标签:__,www,http,Python,self,接口,headers,url,requests From: https://blog.csdn.net/AgostoDu/article/details/142515847