一、简介
Requests是python语言编写,基于urllib,采用Apache2 Licensed开源协议的Http库。
它比urllib更加方便,可以节约大量的工作,完全满足HTTP测试需求。
二、使用
实例引入
import requests
response = requests.get('http://www.baidu.com')
print(response)
print(response.status_code)
print(response.text)
print(type(response.text))
print(response.cookies)
各种请求方式
import requests
requests.post('http://httpbin.org/post')
requests.put('http://httpbin.org/put')
requests.delete('http://httpbin.org/delete')
requests.head('http://httpbin.org/get')
requests.options('http://httpbin.org/get')
请求
GET请求
#基本请求
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
#带参数的GET请求
import requests
response = requests.get('http://httpbin.org/get?name=germey&age=22')
print(response.text)
import requests
data = {
'name' : 'germey',
'age' : 22
}
response = requests.get('http://httpbin.org/get',params = data)
print(response.text)
解析json
import requests
response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json())
print(type(response.json()))
获取二进制数据