目录
一、request介绍与请求
1.request模块介绍
request模块的安装
2.request模块发送请求
(1)简单的发送get请求和get请求说明
?二、request请求参数与响应
1.URL参数与查询参数
2.发送post请求的data参数与json参数
三、cookie与session处理
1.request模块对cookie的处理
2.session类封装cookie
一、request介绍与请求
1.request模块介绍
requests模块是python的第三方模块, 用来发送网络请求,常用于爬虫, 能够完全满足基于HTTP协议的接口测试
request模块的安装
# 安装: pip install requests
# 验证: pip show requests
# 其他请求模块
urllib(古董)
request(好用)
aiohttp: 异步请求模块
2.request模块发送请求
1.请求方法
2.URL地址
3.params,header,body
4.前置脚本,后置脚本
5.响应数据 + 断言结果
(1)简单的发送get请求和get请求说明
# 导包
import request
# 发送请求
request = request.get("http://www.baidu.com")
import requests
# 请求方法:get
# 请求地址:"http://www.baidu.com"
# 响应数据
url = "http://www.baidu.com"
res = requests.get(url)
print(res)
import requests
# 请求方法:get
# 请求地址:"http://www.baidu.com"
# 响应数据
url = "http://www.baidu.com"
res = requests.get(url)
# 编码
res.encoding = "utf-8"
print(res.text)
import requests
# 请求方法:get
# 请求地址:"http://www.baidu.com"
# 响应数据
url = "http://www.baidu.com/s"
params = {
"wd": "黄瓜是绿的为什么叫黄瓜"
}
res = requests.get(url, params=params)
# 编码
res.encoding = "utf-8"
with open('test.html', 'w', encoding='utf-8') as f:
f.write(res.text)
?二、request请求参数与响应
1.URL参数与查询参数
URL参数是请求的网址为字符串形式.
如果是get请求, URL中可以携带查询参数, 使用request模块可以直接将请求参数写在URL中, 也可以通过params参数传递查询参数
2.发送post请求的data参数与json参数
import requests
# 请求方法:get
# 请求地址:"http://www.baidu.com"
# 响应数据
url = "http://www.baidu.com/s"
params = {
"wd": "黄瓜是绿的为什么叫黄瓜"
}
res = requests.get(url, params=params)
# 编码
res.encoding = "utf-8"
with open('test.html', 'w', encoding='utf-8') as f:
f.write(res.text)
import requests
# 请求方法:get
# 请求地址:"http://www.baidu.com"
# 响应数据
url = "http://www.baidu.com/s"
params = {
"wd": "黄瓜是绿的为什么叫黄瓜"
}
hds = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
}
res = requests.get(url, params=params, headers=hds)
# 编码
res.encoding = "utf-8"
with open('test.html', 'w', encoding='utf-8') as f:
f.write(res.text)
import requests
# 确定
url = "http://ihrm-test.itheima.net/api/sys/login"
data = {
"mobile": "13800000002",
"password": "123456"
}
headers = {
"Content-Type": "application/json"
}
res = requests.post(url=url, data=data, headers=headers)
print(res.text)
import requests
url = " http://jgpdf.dlkzbd.top/yuanqidesktop/images/dongman/pic3.jpg"
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
res = requests.get(url, headers=headers)
with open('xxx.jpg', 'wb') as f:
f.write(res.content)
1. 请求方式: requests.请求方法()
2. URL定义: URL是请求的地址,是一个字符串形式的数据
? 做接口测试时,URL应该是接口地址
3. params是URL后面拼接的查询参数,params是以键值对的形式出现的
1. 比如 pms = {'wd' : 'value'}
2. 传参: requests.get(url,params=pms)
4. body: 请求体,请求体一般用在 post/put/patch等方法中
1. ? body 数据一般都是键值对形式的字典
5. 响应数据的类型:
1. res.text: 响应数据的文本形式
2. res.json(): json数据格式转化为python基础数据类型
3. res.content: 获取响应数据的二进制流形式
三、cookie与session处理
1.request模块对cookie的处理
状态保持
import requests
# 请求方法:get
# 请求地址:"http://www.baidu.com"
# 响应数据
url = "http://www.baidu.com/s"
params = {
"wd": "黄瓜是绿的为什么叫黄瓜"
}
hds = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
"Cookie":" BIDUPSID=3C36409D8B19D7898068790C4CA62645; PSTM=1611141832; BAIDUID=2CE147E2FAB7D02F10E3CEB7AA0D5143:FG=1; H_WISE_SIDS=110085_127969_128700_131424_144966_156286_156927_162187_162897_163507_163568_165136_165328_165735_166025_166147_166185_167085_167114_167301_168202_168204_168467_168542_168558_168626_168747_168762_168768_169155_169187_169307_169536_169670_169709_169789_169808_170016_170034_170356_170445_170476_170579_170580_170582_170589; BD_UPN=12314753; __yjs_duid=1_6eeb06f48e2b1dc65fe3d98ff10421b11618400601643; BDUSS_BFESS=hlaXBpUjJ0d1g3cE5JeERhWWJieVhpOU1FdGlBemJ1eGVlM3J2OWtudVVVWUpoRUFBQUFBJCQAAAAAAAAAAAEAAACPIIDOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJTEWmGUxFphd0; ab_sr=1.0.1_Y2EyNzgyZDFhMWMxMzFiNGIzOWUwZDVhMjIzMzg5NTk0ODhlMGI3ZGMwZGFlN2QxYjMzN2UzNGYzNDNhZmM5ZWU3ZjVkMmY5YjU4MWM4YWFlOWM5OTdlYWFmZTVhZjM0OGZkMmQ4ZTE0YjI0Mjk2YTE3ZGIwNDZmMmFlYmJiNGQwYmU3M2U1NmIxM2RkZTg0ZWYyZjk0YzFmZjI2NGU1Yzg5NTgwZGE3ZGRlYmVjNjU3NGFmMTAyOGJjYzZjZWQ3; H_PS_PSSID=31254_26350; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_645EC=88cbaTIiYn0ZHLLfx28Oh7bxGTR3wvUYTnPKkRkWZYjr8hF4NA0vjqRCVfLdLGu4syXyn7YIjC8; BA_HECTOR=8k210h250520ah811h1gm2dvk0r; delPer=0; BD_CK_SAM=1; PSINO=1; BDSVRTM=189"
}
res = requests.get(url, params=params, headers=hds)
# 编码
res.encoding = "utf-8"
with open('test.html', 'w', encoding='utf-8') as f:
f.write(res.text)
2.session类封装cookie
使用步骤:
导包:from request import Session
实例化session对象:session = Session
请求是使用session对象,不在使用request:session.get()
注意事项:要想一直保持状态,中间不可间断,要一直用session请求
import requests
# 请求方法:get
# 请求地址:"http://www.baidu.com"
# 响应数据
url = "http://www.baidu.com/s"
params = {
"wd": "黄瓜是绿的为什么叫黄瓜"
}
hds = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
"Cookie":" BIDUPSID=3C36409D8B19D7898068790C4CA62645; PSTM=1611141832; BAIDUID=2CE147E2FAB7D02F10E3CEB7AA0D5143:FG=1; H_WISE_SIDS=110085_127969_128700_131424_144966_156286_156927_162187_162897_163507_163568_165136_165328_165735_166025_166147_166185_167085_167114_167301_168202_168204_168467_168542_168558_168626_168747_168762_168768_169155_169187_169307_169536_169670_169709_169789_169808_170016_170034_170356_170445_170476_170579_170580_170582_170589; BD_UPN=12314753; __yjs_duid=1_6eeb06f48e2b1dc65fe3d98ff10421b11618400601643; BDUSS_BFESS=hlaXBpUjJ0d1g3cE5JeERhWWJieVhpOU1FdGlBemJ1eGVlM3J2OWtudVVVWUpoRUFBQUFBJCQAAAAAAAAAAAEAAACPIIDOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJTEWmGUxFphd0; ab_sr=1.0.1_Y2EyNzgyZDFhMWMxMzFiNGIzOWUwZDVhMjIzMzg5NTk0ODhlMGI3ZGMwZGFlN2QxYjMzN2UzNGYzNDNhZmM5ZWU3ZjVkMmY5YjU4MWM4YWFlOWM5OTdlYWFmZTVhZjM0OGZkMmQ4ZTE0YjI0Mjk2YTE3ZGIwNDZmMmFlYmJiNGQwYmU3M2U1NmIxM2RkZTg0ZWYyZjk0YzFmZjI2NGU1Yzg5NTgwZGE3ZGRlYmVjNjU3NGFmMTAyOGJjYzZjZWQ3; H_PS_PSSID=31254_26350; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_645EC=88cbaTIiYn0ZHLLfx28Oh7bxGTR3wvUYTnPKkRkWZYjr8hF4NA0vjqRCVfLdLGu4syXyn7YIjC8; BA_HECTOR=8k210h250520ah811h1gm2dvk0r; delPer=0; BD_CK_SAM=1; PSINO=1; BDSVRTM=189"
}
res = requests.get(url, params=params, headers=hds)
# 编码
res.encoding = "utf-8"
with open('test.html', 'w', encoding='utf-8') as f:
f.write(res.text)
|