返回 导航

Python

hangge.com

Python - 第三方HTTP库Requests使用详解1(安装配置、基本用法)

作者:hangge | 2022-06-29 08:02

一、基本介绍

1,什么是 Requests 库?

(1)Requests 是用 python 语言基于 urllib 编写的,采用的是 Apache2 Licensed 开源协议的 HTTP 库。
(2)Requests 使用起来比 urllib 简洁很多,可以说是目前 Python 实现的最简单易用的 HTTP 库。

2,Requests 库功能特性

  • Keep-Alive & 连接池
  • 国际化域名和 URL
  • 带持久 Cookie 的会话
  • 浏览器式的 SSL 认证
  • 自动内容解码
  • 基本/摘要式的身份认证
  • 优雅的 key/value Cookie
  • 自动解压
  • Unicode 响应体
  • HTTP(S) 代理支持
  • 文件分块上传
  • 流下载
  • 连接超时
  • 分块请求
  • 支持 .netrc

3,安装 Requests 库

(1)由于 Requests 是第三方库,所以使用前需要在终端中执行如下命令通过 pip 安装:
pip install requests

(2)由于我的 Mac 电脑又装了最新的 Python 3.7 版本(别名 python3),其自带了 pip,所以执行如下命令安装:
python3 -m pip install requests

二、基本用法

1,发送请求

Requests 为我们提供了各种请求方式:
import requests

requests.get('https://api.github.com/events')
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")

2,传递参数

(1)如果需要传递参数的话,我们可以直接将参数拼接在 URL 后面:
requests.get('http://httpbin.org/get?name=hangge.com&age=100')

(2)也可以使用 params 关键字传递一个字典进来作为参数(参数最终同样会自动拼接到 URL 后面):
import requests
data = {
    "name": "hangge.com",
    "age": 100
}
response = requests.post('http://httpbin.org/post', params=data)
print(response.text)

(3)如果使用 data 关键字传递一个字典进来作为参数,则会以表单的形式传递参数(参数不会拼接到 URL 后面):
import requests
data = {
    "name": "hangge.com",
    "age": 100
}
response = requests.post('http://httpbin.org/post', data=data)
print(response.text)

(4)有时我们想要发送的数据并不需要以表单形式提交。比如传递一个 string 类型的 JSON 串,可以使用 json 参数来传递:
import requests
data = {
    "name": "hangge.com",
    "age": 100
}
response = requests.post('http://httpbin.org/post', json=data)
print(response.text)

三、响应结果

1,获取响应内容

(1)当使用 Requests 发起网络请求时,会返回一个 Response 对象,我们可以通过它来获取响应内容:
import requests

response = requests.get("https://www.baidu.com")

print("\n--- type(response) ---\n", type(response))
print("\n--- response.status_code ---\n", response.status_code) #响应状态码
print("\n--- response.cookies ---\n", response.cookies) #Cookie
print("\n--- type(response.text) ---\n", type(response.text))
print("\n--- response.text ---\n", response.text) #响应文本内容
print("\n--- response.content ---\n", response.content)

(2)很多情况下如果直接输出 response.text 会出现乱码的问题(见上图),要解决这个问题有两种办法:
  • 一是使用 response.content 获取二进制响应内容,然后通过 decode() 转换为 utf-8
import requests

response = requests.get("https://www.baidu.com")

print(response.content.decode("utf-8"))

  • 二是可以使用 response.encoding 属性来指定使用的响应编码:
import requests

response = requests.get("https://www.baidu.com")

response.encoding="utf-8"
print(response.text)

无论上面那种方式,可以看到乱码问题都解决了:

2,JSON 响应内容

(1)如果请求返回的是 JSON 格式的数据,可以使用 Requests 内置的 JSON 解码器,它可以帮助我们处理 JSON 数据:
import requests

response = requests.get('http://httpbin.org/get?name=hangge.com&age=100')
json = response.json()
print(json)
print(json['args']['name'])

(2)当然我们也可以使用 json.loads() 方法手动将 json 字符串转换成 dict 字典,下面代码的效果同上面是一样的:
import requests
import json
response = requests.get("http://httpbin.org/get?name=hangge.com&age=100")

json = json.loads(response.text)
print(json)
print(json['args']['name'])
评论

全部评论(0)

回到顶部