新闻动态

Python HTTP库 requests 的简单使用详情

发布日期:2021-12-30 02:51 | 文章来源:gibhub

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请求处理响应体也非常方便灵活,可以使用的属性有contenttextjson()

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 的简单使用内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部