用于 Python
的下一代 HTTP
客户端。
HTTPX
是 Python 3
的全功能 HTTP
客户端,它提供同步和异步 API
,并支持 HTTP/1.1
和 HTTP/2
。
此库也是借鉴requests
库的思路进行设计的,所以说,你只要会requests
库,那这个库学起来非常顺手。
首先安装第三方包
(venv-python) lifeng@apple bin % pip3 install httpx -i https://pypi.douban.com/simple/
Looking in indexes: https://pypi.douban.com/simple/
Collecting httpx
Downloading https://pypi.doubanio.com/packages/a1/c8/6aef2a5521c76a0bb1a2a0d18829cd36ae13711589b849c536463c8aded8/httpx-0.21.1-py3-none-any.whl (83 kB)
|████████████████████████████████| 83 kB 477 kB/s
Requirement already satisfied: charset-normalizer in /Users/lifeng/venv-python/lib/python3.10/site-packages (from httpx) (2.0.9)
Collecting sniffio
Downloading https://pypi.doubanio.com/packages/52/b0/7b2e028b63d092804b6794595871f936aafa5e9322dcaaad50ebf67445b3/sniffio-1.2.0-py3-none-any.whl (10 kB)
Collecting httpcore<0.15.0,>=0.14.0
Downloading https://pypi.doubanio.com/packages/ac/af/2d3a940b009567ad97cbb97e6387950339dd8552999abe0c6f562f4a9dca/httpcore-0.14.3-py3-none-any.whl (56 kB)
|████████████████████████████████| 56 kB 5.6 MB/s
Requirement already satisfied: certifi in /Users/lifeng/venv-python/lib/python3.10/site-packages (from httpx) (2021.10.8)
Collecting rfc3986[idna2008]<2,>=1.3
Downloading https://pypi.doubanio.com/packages/c4/e5/63ca2c4edf4e00657584608bee1001302bbf8c5f569340b78304f2f446cb/rfc3986-1.5.0-py2.py3-none-any.whl (31 kB)
Collecting h11<0.13,>=0.11
Downloading https://pypi.doubanio.com/packages/60/0f/7a0eeea938eaf61074f29fed9717f2010e8d0e0905d36b38d3275a1e4622/h11-0.12.0-py3-none-any.whl (54 kB)
|████████████████████████████████| 54 kB 784 kB/s
Collecting anyio==3.*
Downloading https://pypi.doubanio.com/packages/ad/0a/919c040f061bc31f604ee8275ada8fa9f6f237425010afa523e429a04a45/anyio-3.4.0-py3-none-any.whl (78 kB)
|████████████████████████████████| 78 kB 976 kB/s
Requirement already satisfied: idna>=2.8 in /Users/lifeng/venv-python/lib/python3.10/site-packages (from anyio==3.*->httpcore<0.15.0,>=0.14.0->httpx) (3.3)
Installing collected packages: sniffio, rfc3986, h11, anyio, httpcore, httpx
Successfully installed anyio-3.4.0 h11-0.12.0 httpcore-0.14.3 httpx-0.21.1 rfc3986-1.5.0 sniffio-1.2.0
其次开始导入httpx
import httpx
现在,开始尝试访问一个网页
import httpx
r = httpx.get("http://www.baidu.com")
print(r)
运行结果
Process finished with exit code 0
同样,然后再发送POST
请求:
import httpx
r = httpx.post("http://www.baidu.com", data={"key": "value"})
print(r)
PUT、DELETE、HEAD
和 OPTIONS
请求都遵循相同的风格:
import httpx
r = httpx.put("http://www.baidu.com", data={"key": "value"})
print(r)
r = httpx.delete("http://www.baidu.com")
print(r)
r = httpx.head("http://www.baidu.com")
print(r)
r = httpx.options("http://www.baidu.com")
print(r)
要在请求中包含 URL
查询参数,请使用params
关键字:
import httpx
params = {
"key": "value"
}
r = httpx.get("http://www.baidu.com", params=params)
如果是POST
请求,也会遇到请求中包含URL
参数,可以直接也使用params
关键字:
import httpx
params = {
"key": "value"
}
r = httpx.post("http://www.baidu.com", params=params)
如果要查看值如何编码到 URL 字符串中,我们可以检查用于发出请求的结果 URL:
print(r.url)
运行结果
http://www.baidu.com?key=value
Process finished with exit code 0
您还可以将列表作为值传递:
import httpx
params = {
"key1": "value",
"key2": ["value1", "value2"]
}
r = httpx.get("http://www.baidu.com", params=params)
print(r.url)
运行结果
http://www.baidu.com?key1=value&key2=value1&key2=value2
Process finished with exit code 0
HTTPX
会自动将响应内容解码为 Unicode
文本:
import httpx
r = httpx.get("http://www.baidu.com")
print(r.text)
也可以检查使用什么编码来解码响应:
import httpx
r = httpx.get("http://www.baidu.com")
print(r.encoding)
运行结果
utf-8
Process finished with exit code 0
如果您需要覆盖标准行为并明确设置要使用的编码,也可以这样做:
import httpx
r = httpx.get("http://www.baidu.com")
r.encoding = 'ISO-8859-1'
print(r.encoding)
运行结果
ISO-8859-1
Process finished with exit code 0
对于非文本响应,响应内容也可以作为字节访问:
import httpx
r = httpx.get("http://www.baidu.com")
print(r.content)
运行结果
b'\n\n\n
通常,Web API 响应将被编码为 JSON:
import httpx
params = {
"key": "4ae76febc9d351326a2381f5a77ce7a2",
"type": "tiyu"
}
r = httpx.get("http://v.juhe.cn/toutiao/index", params=params)
print(r.json())
运行结果
{'reason': 'success!', 'result': {'stat': '1', 'data': [{'uniquekey': 'f08d6ddb99d012c6527fbb818945608d', 'title': '安宁“百花杯”第二日战况来啦!安宁市委办斩获两连胜', 'date': '2021-12-21 22:46:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221224635960683635.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221224635_ca7940308a0a27e37de3d4df4ab0da35_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221224635_ca7940308a0a27e37de3d4df4ab0da35_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221224635_ca7940308a0a27e37de3d4df4ab0da35_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'e912aa4f0cdbf3eb79a8ef3e93a60420', 'title': '濮阳县第四实验小学男子足球队勇夺第六届县长杯校园足球赛小学男子组冠军', 'date': '2021-12-21 22:46:00', 'category': '体育', 'author