Python HTTP库 requests 的简单使用详情
目录
- 1、简单使用
- 2、构建请求查询参数
- 3、构建请求头Headers
- 4、构建POST请求数据
- 4.1 表单数据
- 4.2 json数据
- 5、获取响应内容
- 6、Cookies
- 7、超时配置
- 8、代理
requests库实现了HTTP协议中绝大部分功能,提供了Keep-Alive、连接池、Cookie持久化、HTTP(S)代理支持、连接超时等很多功能特性,最重要的是它同时支持Python2和ython3,而且能在PyPy下完美运行。
使用前需要使用pip install requests命令进行安装。
1、简单使用
res = requests.get("http://httpbin.org/get")
# 状态码
print(res.status_code)
# 响应头
print(res.headers["Content-Type"], res.headers["Server"])
# 响应内容
print(res.text)
执行结果如下:
200
application/json gunicorn/19.9.0
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
.......
},
"origin": "xxx.xxx.xx.xx",
"url": http://httpbin.org/get
}
另外,http请求还有很多类型,比如POST、PUT、DELETE、HEAD、OPTIONS。requests也都可以以简单的方式实现。
res = requests.post("http://httpbin.org/post")
res = requests.put("http://httpbin.org/put")
res = requests.delete("http://httpbin.org/delete")
res = requests.head("http://httpbin.org/get")
res = requests.options("http://httpbin.org/get")
由此看来,使用requests库确实简单方便。
2、构建请求查询参数
很多请求都需要在URL中传递参数,我们可以用字典来构建查询参数,使用params参数在URL中添加参数。
payload = {"wd": "test"}
res = requests.get("https://www.baidu.com/", params=payload)
print(res.url)
运行结果如下:
https://www.baidu.com/?wd=test
3、构建请求头Headers
requests可以在请求中很简单的指定请求头的Headers信息,直接传递一个字典给参数headers即可。
headers = {"user-agent": "Mozilla/5.0", "cookies": "xxx"}
res = requests.get("https://www.baidu.com/", headers=headers)
4、构建POST请求数据
requests可以非常方便的构建POST请求需要的数据。如果服务端接收的的数据是表单数据,可以使用参数data上送,如果接收的是json格式的数据,则可以使用json参数上送。
4.1 表单数据
import requests
data = {"key1": "value1", "key2": "value2"}
res = requests.post("http://httpbin.org/post", data=data)
print(res.text)
运行结果如下:
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.26.0",
"X-Amzn-Trace-Id": "Root=1-614d7d91-559333ee19237f845026ef37"
},
"json": null,
"origin": "xxx.xxx.xx.xx",
"url": "http://httpbin.org/post"
}
4.2 json数据
import json
import requests
url = "http://httpbin.org/post"
data = {"key": "value"}
data = json.dumps(data)
res = requests.post(url, data=data)
print(res.text)
运行结果如下:
{
"args": {},
"data": "{\"key\": \"value\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "16",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.26.0",
"X-Amzn-Trace-Id": "Root=1-614d7e91-065887f925dce94d6d03b2e4"
},
"json": {
"key": "value"
},
"origin": "xxx.xxx.xx.xx",
"url": "http://httpbin.org/post"
}
5、获取响应内容
使用requests请求处理响应体也非常方便灵活,可以使用的属性有content、text、json() 。
content属性获取的是byte类型的数据。
import requests
res = requests.get("http://httpbin.org/get")
print(res.content)
text属性获取的是str类型的数据。
import requests
res = requests.get("http://httpbin.org/get")
print(res.text)
如果返回的内容是json格式的数据时,就可以使用json()方法返回一个经过json.loads()处理后的对象。
import requests url = "http://httpbin.org/post" res = requests.post(url) print(res.json())
6、Cookies
如果响应中包含了cookie信息,我们可以使用cookies属性获取。
res = requests.get("http://httpbin.org/get")
print(res.cookies)
另外还可以使用cookies参数向服务端发送cookies信息。
url = "http://httpbin.org/cookies"
cookies = {"cookies": "xxxxx"}
r = requests.get(url, cookies=cookies)
print(r.text)
7、超时配置
可以利用timeout参数来配置最大请求时间。
requests.get("https://baidu.com", timeout=0.01)
8、代理
如果需要使用代理,我们可以通过proxies参数来配置。
import requests
proxies = {
'http': 'http://175.7.199.202:3256',
'https': 'http://175.7.199.59:3256',
}
requests.get('http://httpbin.org/get', proxies=proxies)
总结:
到此这篇关于Python HTTP库 requests 的简单使用详情的文章就介绍到这了,更多相关Python HTTP库 requests 的简单使用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
