request是python第三方库,用来发送网络请求,常用于爬虫,能够完全满足基于http协议的接口测试。
#模块安装与查看:
pip install requests
pip show requests
我们在用postman测试时,基本步骤如下:
接下来我们来做一个requests请求百度首页的简单请求吧!
import requests
# 请求方法:get
# 请求地址:http://www.baidu.com
# 响应数据
url = "http://www.baidu.com"
res = requests.get(url)
# 编码
res.encoding = "utf-8"
print(res.text)
运行后得到一个关于百度首页的HTML页面内容,如图:
那么有了这个基础,我们具体在百度中搜索内容,怎么实现呢?
之前我们在postman中,会输入网址+s?wd=xxx来实现,在这里也是同理。
# 请求方法:requests方法
# URL定义:URL是请求的地址,是一个字符串形式数据
# 做接口测试时,URL应该是接口地址
# params是URL后面拼接的查询参数,params以键值对
# 形式出现的 http://www.baidu.com/s?wd=枕上诗书
import requests
# 响应数据
url = "http://www.baidu.com/s?"
params = {
"wd":"枕上诗书,听听诗中的故事"
}
res = requests.get(url,params=params)
# 编码
res.encoding = "utf-8"
with open('shige.html','w',encoding="utf-8") as f:
f.write(res.text)
生成的文档如图:
学会了以上操作,你就会简单的爬虫了,惊不惊喜!
headers
import requests
# 请求方法: get
# 请求地址: http://www.baidu.com
# 响应数据
url = "http://www.baidu.com/s?"
params = {
"wd": "黄瓜是绿的为什么叫黄瓜?"
}
hds = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
"Cookie": "BIDUPSID=FAFFDE677D32BE41E947044960BDEC80; PSTM=1626184441; BAIDUID=FAFFDE677D32BE41A0FB78288514E188:FG=1; BD_UPN=123253; __yjs_duid=1_38b5a097618ee9b504056f99d75fda351626269216400; MCITY=-131%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDUSS=35nUXdyWHZ5cWtMNXpjNGhuZ1gycEZnVWM2N3dXLTByM2t4MGl4aERYZXNLWHRoRVFBQUFBJCQAAAAAAAAAAAEAAABwfMtW09rQodPjMDgyMGZyZWUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKycU2GsnFNhU; BDUSS_BFESS=35nUXdyWHZ5cWtMNXpjNGhuZ1gycEZnVWM2N3dXLTByM2t4MGl4aERYZXNLWHRoRVFBQUFBJCQAAAAAAAAAAAEAAABwfMtW09rQodPjMDgyMGZyZWUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKycU2GsnFNhU; delPer=0; BD_CK_SAM=1; BAIDUID_BFESS=94073EC4D7221D4A3A8DD99152393D6B:FG=1; BD_HOME=1; shifen[260820462938_68429]=1633701904; BCLID=6866293855574678326; BDSFRCVID=SW-OJeC62ACAXaTHDbuteit10XDECVrTH6aoDKyvs5f9ft6MD2xBEG0PEf8g0Kub2V-DogKK0mOTHv8F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF=tJAeVIDMtKI3HJ-k-JO55bLO2eT22-usLakL2hcH0KLKEqOqW-QxyM0V5qO-aR0DtNbRKlOC-fb1MRjvQMRB5RJ0bGrh-T5u3nrO5q5TtUJpeCnTDMRh-6DZqtOyKMnitIj9-pnKfpQrh459XP68bTkA5bjZKxtq3mkjbPbDfn02eCKu-n5jHjjWjNKe3H; BCLID_BFESS=6866293855574678326; BDSFRCVID_BFESS=SW-OJeC62ACAXaTHDbuteit10XDECVrTH6aoDKyvs5f9ft6MD2xBEG0PEf8g0Kub2V-DogKK0mOTHv8F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tJAeVIDMtKI3HJ-k-JO55bLO2eT22-usLakL2hcH0KLKEqOqW-QxyM0V5qO-aR0DtNbRKlOC-fb1MRjvQMRB5RJ0bGrh-T5u3nrO5q5TtUJpeCnTDMRh-6DZqtOyKMnitIj9-pnKfpQrh459XP68bTkA5bjZKxtq3mkjbPbDfn02eCKu-n5jHjjWjNKe3H; COOKIE_SESSION=5045_1_5_5_20_9_1_0_5_5_12_0_5164_0_121_3_1633702023_1633701905_1633701902%7C9%2391615_26_1633701902%7C9; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=6; sugstore=0; H_PS_PSSID=34645_34528_34067_31253_34712_34525_34584_34505_34709_34107_26350; H_PS_645EC=054aBrX4najJi9Q%2FvR8SUUOL%2BMr6wBKVKzKHz9QpnQ7unO8C2fRGWereyJk; BDSVRTM=219; BA_HECTOR=81252ka284aha5a5or1gm2bps0r"
}
res = requests.get(url=url, params=params, headers=hds)
# 编码
res.encoding = "utf-8"
with open('test.html', 'w', encoding='utf-8') as f:
f.write(res.text)
ret = requests.get(url=url, params=params, headers=hds)
# 1.请求方法: requests.请求方法()
# 2.URL定义: URL是请求的地址, 是一个字符串形式数据
# 做接口测试时, URL应该是接口地址
# 3.params是URL后面拼接的查询参数, params是以键值对
# 形式出现的, 比如: pms = {'wd': "value"}
# 传参: requests.get(url, params=pms)
# 4.body: 请求体, 请求体一般用在post/put/patch等方法中
# body数据一般都是键值对形式的字典
# 5.响应数据的类型:
# - res.text: 响应数据的文本形式
# - res.json(): json数据格式转化为python基础数据类型
# - res.content: 获取响应数据的二进制流形式
手机号登录实现:
import requests
# 确定URL
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(type(res.json()))
上传图片:
import requests
url = "https://th.wallhaven.cc/small/73/73xo19.jpg"
headers = {
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
}
res = requests.get(url, headers=headers)
with open('xxx.jpg', 'wb') as f:
f.write(res.content)
cookie与session的处理
前面我们知道由于http协议是无状态的,所以想要两个请求之间有状态的保持,我们需要使用cookie来达到目的。
session类自动封装cookie
多个接口的保持,每次每次传递cookie参数比较麻烦,我们可以借助requests模块提供session类,该类可以自动封装cookie,实现所有用户之间的状态保持,但需要注意,一旦使用session类后不能中途使用requests直接发送请求,这会中断cookie的状态保
举例:
# 使用步骤
# 导包: from requests import Session
# 实例化session对象: session = Session()
# 请求是使用session对象, 不在使用requests: session.get()
# 注意事项: 要想一直保持状态, 中间不可间断, 要一直用session请求
from requests import Session
# 实例化Session对象
session = Session()
url_login = 'http://www.2552.com.cn/e/member/doaction.php'
data = {
'ecmsfrom': '',
'enews': 'login',
'tobind': '0',
'username': 'Jeremy',
'password': '123456',
'lifetime': '0',
'Submit': '登 录'
}
session.post(url=url_login, data=data)
url_mine = 'http://www.2552.com.cn/e/member/cp/'
res = session.get(url=url_mine)
with open('./mypage.html', 'w', encoding='utf8') as f:
f.write(res.text)