Requests 库是Python基于 urllib实现的,但它比 urllib 更加方便,可以帮助我们更方便地进行HTTP请求,完全满足 HTTP的测试需求。
安装requests库:
#别忘了加s
pip install requests
1.get请求
以http://www.baidu.com为例
传递url,params默认是None(请求数据)
import requests
url='http://www.baidu.com'
res=requests.get(url)#发起一个get请求
print(res)#返回的响应对象
结果:
<Response [200]>
print(res.content)#以二进制形式返回
结果:
b'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/
print(res.status_code)#返回的响应状态码
结果:
200
print(res.headers)#获取响应头信息
结果:
{
'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform',
'Connection': 'keep-alive',
'Content-Encoding': 'gzip',
'Content-Type': 'text/html',
'Date': 'Mon, 08 Jun 2020 08:33:12 GMT',
'Last-Modified': 'Mon, 23 Jan 2017 13:27:32 GMT',
'Pragma': 'no-cache',
'Server': 'bfe/1.0.8.18',
'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}
这里需要注意的是,此时响应结果只接收text/html,
所以如果以json格式返回的话会报错:
print(res.json())
结果:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
带参数的get请求:
以请求http://httpbin.org/网站为例
请求参数以字典的形式做为params的参数传入
import request
url='http://httpbin.org/get'
data={
'name': 'python',
'age' : 18
}
res=requests.get(url,params=data)
请求的参数会自动拼接到url后面,请求后得到相应的请求结果
print(res.text)
结果:
{
"args": {
"age": "17",
"name": "python"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5edde90a-6c04d8f0ed840be0c8bf1490"
},
"origin": "123.112.246.165",
"url": "http://httpbin.org/get?name=python&age=17"
}
2.post请求
传递参数的格式可以是josn或data;如下data是以form表单的形式传递,用data接收请求的数据做为请求参数。
import request
url='http://httpbin.org/get'
info={
'name': 'python',
'age' : 18
}
res=requests.post(url,data=info)
请求参数在form中,并且headers头信息中的Content-Type默认为application/x-www-form-urlencoded表单形式。
print(res.text)
结果:
{
"args": {},
"data": "",
"files": {},
"form": {
"age": "17",
"name": "python"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "18",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5eddeeae-6446d28447a50134f6029e36"
},
"json": null,
"origin": "123.112.246.165",
"url": "http://httpbin.org/post"
}
如果需要传递json数据,需要用json接收data数据做为请求参数
import request
url='http://httpbin.org/get'
info={
'name': 'python',
'age' : 18
}
res=requests.get(url,json=info)
此时请求参数在data中,headers头信息中的Content-Type默认为application/json格式。
print(res.text)
结果:
{
"args": {},
"data": "{\"name\": \"python\", \"age\": 17}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "29",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5eddf1c8-21ef7f06b2899aae6f4aa362"
},
"json": {
"age": 17,
"name": "python"
},
"origin": "123.112.246.165",
"url": "http://httpbin.org/post"
}
至于使用哪种方式需要有后端开发规定,有的接口支持form表单格式,有的接口只支持json格式。
如果需要定制请求头信息,需要用headers接收请求头的信息作为请求参数;
如下为post请求时,未指定请求头,信息为默认的。
以User-Agent为例,此时的"User-Agent":为"python-requests/2.23.0"。如果指定为其他的客户端如下:
import requests
data={
'name':'python',
'age': 17
}
headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
res = requests.post('http://httpbin.org/post',json=data,headers=headers)
print(res.text)
结果:
{
"args": {},
"data": "{\"name\": \"python\", \"age\": 17}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "29",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19",
"X-Amzn-Trace-Id": "Root=1-5eddfb47-403c0b7a09b24ba89919c9d8"
},
"json": {
"age": 17,
"name": "python"
},
"origin": "123.112.246.165",
"url": "http://httpbin.org/post"
}
请求格式为json,但是也能通过text的格式获取,结果为str类型;json()为dict类型。
通过text获取响应内容时不显示中文:
response = requests.get(url)
response.encoding = response.apparent_encoding
print(response.text)