简介

用于 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 httpxDownloading 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 sniffioDownloading 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.0Downloading 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.3Downloading https://pypi.doubanio.com/packages/c4/e5/63ca2c4edf4e00657584608bee1001302bbf8c5f569340b78304f2f446cb/rfc3986-1.5.0-py2.py3-none-any.whl (31 kB)
Collecting h11<0.13,>=0.11Downloading 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 httpxr = httpx.get("http://www.baidu.com")
print(r)

运行结果

<Response [200 OK]>Process finished with exit code 0

同样,然后再发送POST请求:

import httpxr = httpx.post("http://www.baidu.com", data={"key": "value"})
print(r)

PUT、DELETE、HEAD 和 OPTIONS 请求都遵循相同的风格:

import httpxr = 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 中传递参数

要在请求中包含 URL 查询参数,请使用params关键字:

import httpxparams = {"key": "value"
}
r = httpx.get("http://www.baidu.com", params=params)

如果是POST请求,也会遇到请求中包含URL参数,可以直接也使用params关键字:

import httpxparams = {"key": "value"
}r = httpx.post("http://www.baidu.com", params=params)

如果要查看值如何编码到 URL 字符串中,我们可以检查用于发出请求的结果 URL:

print(r.url)

运行结果

http://www.baidu.com?key=valueProcess finished with exit code 0

您还可以将列表作为值传递:

import httpxparams = {"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=value2Process finished with exit code 0

返回内容

HTTPX 会自动将响应内容解码为 Unicode 文本:

import httpxr = httpx.get("http://www.baidu.com")
print(r.text)

也可以检查使用什么编码来解码响应:

import httpxr = httpx.get("http://www.baidu.com")
print(r.encoding)

运行结果

utf-8Process finished with exit code 0

如果您需要覆盖标准行为并明确设置要使用的编码,也可以这样做:

import httpxr = httpx.get("http://www.baidu.com")
r.encoding = 'ISO-8859-1'
print(r.encoding)

运行结果

ISO-8859-1Process finished with exit code 0

二进制响应内容

对于非文本响应,响应内容也可以作为字节访问:

import httpxr = httpx.get("http://www.baidu.com")
print(r.content)

运行结果

b'<!DOCTYPE html><!--STATUS OK-->\n\n\n    <html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="theme-color" content="#ffffff"><meta name="description" ......Process finished with exit code 0

JSON 响应内容

通常,Web API 响应将被编码为 JSON:

import httpxparams = {"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_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221224635873249134.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221224635_459ec33f700258117d0fa30d24763f11_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221224635_459ec33f700258117d0fa30d24763f11_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221224635_459ec33f700258117d0fa30d24763f11_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '469ca1b349a145bcf06c3901c21f680d', 'title': '打破亚洲纪录!中国队获短池游泳世锦赛女子4x50米自由泳接力赛决赛第四名', 'date': '2021-12-21 22:30:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221223053043923107.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221223053_5acf781435b6629414af7a95309e660c_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '4389a8af7f8ba5500867d2710f2b582e', 'title': '两连胜!中国国象甲级联赛深圳鹏城队积分榜上升至第7位', 'date': '2021-12-21 22:23:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221222330118730550.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221222330_8b5710ba73272cf9ed9ecb6ea873c4fb_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221222330_8b5710ba73272cf9ed9ecb6ea873c4fb_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221222330_8b5710ba73272cf9ed9ecb6ea873c4fb_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '5a842f1712060ec6a37eb57606b6418d', 'title': '中超|青岛与津门虎双双落败 难兄难弟继续“垫底”', 'date': '2021-12-21 22:15:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221221507298998179.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221221507_8d599c109d5d522a54c12231255a6afa_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221221507_8d599c109d5d522a54c12231255a6afa_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '191f43f6834bd8d11470ebce93c25bf8', 'title': '【中超】不能争冠也要力争上游,广州城明晚PK河北队', 'date': '2021-12-21 22:15:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221221503373432817.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221221503_ac2731b1ec1eba7897a6a31edae1c2ad_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221221503_ac2731b1ec1eba7897a6a31edae1c2ad_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'd09178313314066b06bb0f35fb1f878f', 'title': '摘金时刻,团泊体育中心沸腾了!', 'date': '2021-12-21 21:59:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221215956161264894.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221215956_57aa7f69a4fed23d162ae8e82c39e8ba_2_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221215956_57aa7f69a4fed23d162ae8e82c39e8ba_3_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221215956_57aa7f69a4fed23d162ae8e82c39e8ba_4_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '149e33f4dc9de8582626a29d28a8e862', 'title': '郝伟透露三名球员伤病情况 期待孙准浩能有更多进球', 'date': '2021-12-21 21:59:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221215927152774342.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221215927_79937d7a9bf87e7d170e793945027d6a_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'be76edebf6e86b70b565f40cdb3b8297', 'title': '张家口冬奥村(冬残奥村)举行全要素测试', 'date': '2021-12-21 21:44:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221214413146190947.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221214413_ef905c250ea1c41ac46918809048c7d9_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'f8ef4ac6dfe2b1292de9afdb48023822', 'title': '长宁“小囡”唐钱婷勇夺世锦赛金牌!', 'date': '2021-12-21 21:36:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221213600151760522.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221213600_48331a61acbe811c99c6f04e85cc6424_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221213600_48331a61acbe811c99c6f04e85cc6424_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221213600_48331a61acbe811c99c6f04e85cc6424_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'e80969502449590997f96659fc452723', 'title': '中超|比利奇结束停赛归来 排出最强阵迎战下一场', 'date': '2021-12-21 21:20:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221212036665553638.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221212036_9ae980bd97f7f261e4db4a67c98a3b98_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '4d034b617ae0409b458c5bdc9ae4c7b4', 'title': '郝伟:山东泰山积分领先不代表稳夺冠,张弛跟队参加了热身', 'date': '2021-12-21 21:12:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221211212557767903.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221211212_953a3a50c4f9a5ab3655c7738ecf5f31_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '1561179a2c9ca6fda8aa12ba3284495b', 'title': '体坛新声 | 中超天王山之战裁判不抢戏,球员成为真正主角', 'date': '2021-12-21 20:48:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221204836771583791.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221204836_67c4785f6bb93001851483590f0bbe3a_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221204836_67c4785f6bb93001851483590f0bbe3a_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221204836_67c4785f6bb93001851483590f0bbe3a_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'fdb390a241d16f452202ce028991f7a6', 'title': '2021年首届中国美人鱼表演赛在三亚亚特兰蒂斯开幕', 'date': '2021-12-21 20:40:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221204050338371797.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221204050_6847497e55f87b0ee901cdbd3f4cad44_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221204050_6847497e55f87b0ee901cdbd3f4cad44_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221204050_6847497e55f87b0ee901cdbd3f4cad44_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '3eb327020bf298ed355b4004ba957328', 'title': '意甲第19轮尤文图斯VS卡利亚里前瞻,欧宝体育尤文获胜无悬念', 'date': '2021-12-21 20:39:00', 'category': '体育', 'author_name': '运动分子', 'url': 'https://mini.eastday.com/mobile/211221203930370787517.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/minimodify/20211221/1269x1269_61c1ca4560785_mwpmwm_03201609.png', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/minimodify/20211221/1280x1280_61c1ca4886d8d_mwpmwm_03201609.png', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/minimodify/20211221/1181x865_61c1ca4b58f99_mwpmwm_03201609.png', 'is_content': '1'}, {'uniquekey': '2e105929cd343647908e8d394acce720', 'title': '中甲联赛成都蓉城大胜贵州队 将参加附加赛决定是否能冲上中超', 'date': '2021-12-21 20:17:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221201711820555994.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221201711_6ad9a9061012f10280f12a15a1715751_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221201711_6ad9a9061012f10280f12a15a1715751_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '5e3368768619282c29efcbfa75b6c887', 'title': '上海女足又一队员留洋 杨淑慧加盟法国夏朗德苏瓦奥俱乐部', 'date': '2021-12-21 20:01:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221200138107331311.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221200138_fedd8fc32ed7b52fcede505f32fa89c8_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'df61c2efa5a47aad94de76b71a02d9c3', 'title': '中超|嵩山龙门取得第2阶段首胜 武汉队同阶段保持不败', 'date': '2021-12-21 19:53:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221195341389350169.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221195341_632fd7f69e6053f2ca4bc5e8493aab6e_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '3fb79a8c7229997c81e6d885a7b29e9b', 'title': 'WTT 澳门冠军赛下月开战,国乒东奥阵容全员参与', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194516312975061.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194516_3d030ff503d988a9bf9fd8a0d6d2b1a6_1_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '2e2652a61dc5b7f56c9fb2dc637df5c5', 'title': '首届川渝中老年“三好杯”足球赛圆满落幕', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194552865881259.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194552_a23a8e4194e88461d82689a802167ece_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194552_a23a8e4194e88461d82689a802167ece_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194552_a23a8e4194e88461d82689a802167ece_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'ff3e082558d34406e355a888b1fdd1cf', 'title': '迎接北京冬奥 四川省全民健身冰雪季来啦', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194549407794170.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194549_402dfe3299f13ca2965ebddffc34e941_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194549_402dfe3299f13ca2965ebddffc34e941_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '9edd0bbf5e3a12ae582244c222cbd84c', 'title': '首钢备战CBA第二阶段 林书豪像鼓手 掌握攻守节奏', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194545968947131.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194545_7b6f40a09c47d1e6cb410cd7feba1ca6_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194545_7b6f40a09c47d1e6cb410cd7feba1ca6_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194545_7b6f40a09c47d1e6cb410cd7feba1ca6_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'a471724406f47dcad4825a17fd8a2c3c', 'title': '国家体育馆举行首次冬奥赛时颁奖模拟演练', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194525484687781.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194525_f9aa864282d674aadae2678822d9fc0a_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194525_f9aa864282d674aadae2678822d9fc0a_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '27e41fe04812de006c28de631cd6a6c2', 'title': '中国男子手球超级联赛 北京体彩赢得“京鲁大战”', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194524673344013.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194524_d8fdfa94f78e580cb530e93210cb7346_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194524_d8fdfa94f78e580cb530e93210cb7346_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194524_d8fdfa94f78e580cb530e93210cb7346_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '58fdfc9c01d3cf7485e695f596e894e9', 'title': '平凉籍运动名将逯艳受邀担任甘肃冬春旅游宣传推广大使', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194515861868634.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194515_e506d689d5d3e342a7c9e6232ed56cd9_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194515_e506d689d5d3e342a7c9e6232ed56cd9_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194515_e506d689d5d3e342a7c9e6232ed56cd9_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '18db997668a5c22853cb92b7c01f3593', 'title': '借力产业集群优势 福建泉州培育高质量健身休闲企业', 'date': '2021-12-21 19:45:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221194514904152134.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194514_5931f6dd6ed9edc13340c70bccff06fa_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221194514_5931f6dd6ed9edc13340c70bccff06fa_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'e1f5746e417e3875e62781953f80aadd', 'title': '新知|冰雪五环桥、变角斜行梯……揭秘北京冬奥会中的江苏智造', 'date': '2021-12-21 19:37:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221193750358523669.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221193750_0f2c76f2b56c07085ff65d999f4aceb2_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221193750_0f2c76f2b56c07085ff65d999f4aceb2_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'd344f196bfaa93957cb1a59397bb3b1c', 'title': '国内三大帆船赛事海口落幕 助推海口打造冬训天堂', 'date': '2021-12-21 19:29:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221192946047613568.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221192946_f7d0ccb8fa1827dd8427f22581bc67d1_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221192946_f7d0ccb8fa1827dd8427f22581bc67d1_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221192946_f7d0ccb8fa1827dd8427f22581bc67d1_3_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': '74a3ead793b476b9ffee9a6f8c5fb0aa', 'title': '青岛队对阵大连人队比赛名单出炉,“鸭梨”领衔新援巴杜进替补阵容', 'date': '2021-12-21 19:21:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221192147740506890.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221192147_db63715adb7a5a15ab7c349383391022_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221192147_db63715adb7a5a15ab7c349383391022_2_mwpm_03201609.jpeg', 'is_content': '1'}, {'uniquekey': 'e28fbc993cb70d877d945026f722df95', 'title': '北京冬奥会临近,网红记者王冰冰晒冬至滑雪vlog', 'date': '2021-12-21 19:14:00', 'category': '体育', 'author_name': '人民资讯', 'url': 'https://mini.eastday.com/mobile/211221191416108293888.html', 'thumbnail_pic_s': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221191416_d1d36ffbc6367e3305ca60e0aaa95773_1_mwpm_03201609.jpeg', 'thumbnail_pic_s02': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221191416_d1d36ffbc6367e3305ca60e0aaa95773_2_mwpm_03201609.jpeg', 'thumbnail_pic_s03': 'https://dfzximg02.dftoutiao.com/news/20211221/20211221191416_d1d36ffbc6367e3305ca60e0aaa95773_3_mwpm_03201609.jpeg', 'is_content': '1'}], 'page': '1', 'pageSize': '30'}, 'error_code': 0}Process finished with exit code 0

自定义头部信息

要在传出请求中包含其他标头,请使用headers关键字参数:

import httpxheaders = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36"
}r = httpx.get("http://v.juhe.cn/toutiao/index", headers=headers)

发送表单编码数据

某些类型的 HTTP 请求,例如POSTPUT请求,可以在请求正文中包含数据。一种常见的包含方式是作为表单编码数据,用于 HTML 表单:

import httpxdata = {"key": "4ae76febc9d351326a2381f5a77ce7a2","type": "tiyu","is_filter": 1
}r = httpx.post("http://v.juhe.cn/toutiao/index", data=data)
print(r.text)

表单编码数据还可以包含来自给定键的多个值:

import httpxdata = {"key": "4ae76febc9d351326a2381f5a77ce7a2","type": "tiyu","is_filter": 1,"value": ["value01", "value02"]
}r = httpx.post("http://v.juhe.cn/toutiao/index", data=data)
print(r.text)

发送分段文件上传

您可以使用 HTTP 多部分编码上传文件:

import httpx
from pathlib import Pathheaders = {"token": "e2a3ba5dr3bbb78a6e597d3f15e85861"}
url = "https://first.com/company/department/excel"
path = Path(__file__).parent.joinpath("导入部门.xlsx")
files = {'file': open(path, 'rb')}
r = httpx.post(url, headers=headers, files=files)

您也可以通过使用文件值的项目元组来显式设置文件名和内容类型:

import httpx
from pathlib import Pathheaders = {"token": "e2a3ba5dr3bbb78a6e597d3f15e85861"}
url = "https://first.com/company/department/excel"
path = Path(__file__).parent.joinpath("导入部门.xlsx")
files = {'file': ('导入部门.xls', open(path, 'rb'), 'application/vnd.ms-excel')}
r = httpx.post(url, headers=headers, files=files)

如果您需要在多部分表单中包含非文件数据字段,请使用以下data=...参数:

import httpx
from pathlib import Pathheaders = {"token": "e2a3ba5dr3bbb78a6e597d3f15e85861"}
url = "https://first.com/company/department/excel"
path = Path(__file__).parent.joinpath("导入部门.xlsx")
data = {'message': 'Hello, world!'
}
files = {'file': ('导入部门.xls', open(path, 'rb'), 'application/vnd.ms-excel')}
r = httpx.post(url, headers=headers, data=data, files=files)

发送 JSON 编码数据

如果您只需要一个简单的键值数据结构,那么表单编码数据就可以了。对于更复杂的数据结构,推荐改用 JSON 编码:

import httpxdata = {"key": "4ae76febc9d351326a2381f5a77ce7a2","type": "tiyu","is_filter": 1,"list": [1, 2, 3, 4],"data": {"list": ["1","2","3"]}
}r = httpx.post("http://v.juhe.cn/toutiao/index", json=data)

发送二进制请求数据

对于其他编码,可以使用content=...参数,传递一个bytes类型或一个生成器bytes:

import httpxcontent = b"hello world" or bytes("hello world")r = httpx.post("http://v.juhe.cn/toutiao/index", content=content)
print(r.content)

响应状态代码

我们可以检查响应的 HTTP 状态代码:

import httpxdata = {"key": "4ae76febc9d351326a2381f5a77ce7a2","type": "tiyu","is_filter": 1,
}r = httpx.post("http://v.juhe.cn/toutiao/index", data=data)
print(r.status_code)

运行结果:

200Process finished with exit code 0

HTTPX库还包括一个通过文本短语访问状态代码的简单快捷方式:

import httpxparams = {"key": "4ae76febc9d351326a2381f5a77ce7a2",
}r = httpx.get("http://v.juhe.cn/toutiao/index", params=params)
print(httpx.codes.OK == r.status_code)

运行结果:

TrueProcess finished with exit code 0

也可以为任何不是2xx成功代码的响应引发异常:

import httpxparams = {"key": "4ae76febc9d351326a2381f5a77ce7a2",
}r = httpx.get("http://v.juhe.cn/toutiao/index/404", params=params)
print(r.raise_for_status())

运行结果:

Traceback (most recent call last):File "/Users/lifeng/python-projects/python-code/playwrightProject/example.py", line 16, in <module>print(r.raise_for_status())File "/Users/lifeng/venv-python/lib/python3.10/site-packages/httpx/_models.py", line 1508, in raise_for_statusraise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Client error '404 Not Found' for url 'http://v.juhe.cn/toutiao/index/404?key=4ae76febc9d351326a2381f5a77ce7a2'
For more information check: https://httpstatuses.com/404Process finished with exit code 1

成功的响应代码将简单地返回None而不是引发异常:

import httpxparams = {"key": "4ae76febc9d351326a2381f5a77ce7a2",
}r = httpx.get("http://v.juhe.cn/toutiao/index", params=params)
print(r.raise_for_status())

运行结果:

NoneProcess finished with exit code 0

响应头

响应头可用作类似字典的界面。

r.headers
Headers({'content-encoding': 'gzip','transfer-encoding': 'chunked','connection': 'close','server': 'nginx/1.0.4','x-runtime': '148ms','etag': '"e1ca502697e5c9317743dc078f67693f"','content-type': 'application/json'
})

Headers数据类型是不区分大小写的,所以你可以使用任何数据:

r.headers['Content-Type']
'application/json'r.headers.get('content-type')
'application/json'

流响应

对于大型下载,可能希望使用不会一次将整个响应正文加载到内存中的流式响应。

这里可以使用流式传输响应的二进制内容…

with httpx.stream("GET", "https://www.baidu.com") as r:for data in r.iter_bytes():print(data)

或者回复的正文…

with httpx.stream("GET", "https://www.baidu.com") as r:for text in r.iter_text():print(text)

或逐行流式传输文本…

with httpx.stream("GET", "https://www.baidu.com") as r:for line in r.iter_lines():print(line)

Cookie

可以轻松访问响应中设置的任何 cookie:

r = httpx.get('https://baidu.com/cookies/set?chocolate=chip')
r.cookies['chocolate']
'chip'

要在传出请求中包含 cookie,请使用以下cookies参数:

cookies = {"peanut": "butter"}
r = httpx.get('https://baidu.com/cookies', cookies=cookies)
r.json()
{'cookies': {'peanut': 'butter'}}

Cookies 在一个Cookies实例中返回,它是一个类似dict的数据结构,带有额外的API,用于通过它们的域或路径访问 cookie

cookies = httpx.Cookies()
cookies.set('cookie_on_domain', 'hello, there!', domain='httpbin.org')
cookies.set('cookie_off_domain', 'nope.', domain='example.org')
r = httpx.get('http://baidu.com/cookies', cookies=cookies)
r.json()
{'cookies': {'cookie_on_domain': 'hello, there!'}}

超时

HTTPX 默认为所有网络操作包括合理的超时,这意味着如果连接没有正确建立,那么它应该总是引发错误而不是无限期挂起。

网络不活动的默认超时为五秒。您可以将值修改为或多或少严格:

httpx.get('https://baidu.com/', timeout=0.001)

您还可以完全禁用超时行为…

httpx.get('https://baidu.com/', timeout=None)

当然,也是可以配置超时行为的,这个后面的进阶教程中也会罗列出来。


重定向和历史

默认情况下,HTTPX不会对所有HTTP方法进行重定向,尽管这可以显式启用。

例如,GitHub将所有HTTP请求重定向到HTTPS

import httpxr = httpx.get('http://github.com/')
print(r.status_code)
print(r.history)
print(r.next_request)

运行结果:

301
[]
<Request('GET', 'https://github.com/')>Process finished with exit code 0

当然,你也可以使用以下follow_redirects参数修改默认重定向处理:

import httpxr = httpx.get('http://github.com/', follow_redirects=True)
print(r.status_code)
print(r.history)
print(r.next_request)

运行结果:

200
[<Response [301 Moved Permanently]>]
NoneProcess finished with exit code 0

history响应的属性可用于检查任何跟随的重定向。它包含遵循的任何重定向响应的列表,按响应的顺序排列。


以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;非常期待你的点赞和分享哟,谢谢!

未完,待续…

一直都在努力,希望您也是!

微信搜索公众号:就用python

文章作者:李 锋|编辑排版:梁莉莉

更多内容欢迎关注公众号

python:HTTPX 库的快速开始相关推荐

  1. python开源库——h5py快速指南

    1. 核心概念 一个HDF5文件是一种存放两类对象的容器:dataset和group. Dataset是类似于数组的数据集,而group是类似文件夹一样的容器,存放dataset和其他group.在使 ...

  2. python符号格式化设置区间_Python 数值区间处理_对interval 库的快速入门详解

    使用 Python 进行数据处理的时候,常常会遇到判断一个数是否在一个区间内的操作.我们可以使用 if else 进行判断,但是,既然使用了 Python,那我们当然是想找一下有没有现成的轮子可以用. ...

  3. python相关库快速下载

    Python相关扩展库的快速下载 win+r 输入cmd 按enter键 2.安装pqi---可以快速切换镜像源,是快速的关键 3. 4.选择使用镜像源 我使用的是清华大学的清华源 5.然后即可使用p ...

  4. 利用清华镜像快速安装Python第三方库

    利用清华镜像快速安装Python第三方库(以cv库为例) 打开cmd 以opencv-python示例 pip install -i https://pypi.tuna.tsinghua.edu.cn ...

  5. 使用Python第三方库xlwings将单个excel工作表快速拆分为多个工作表(附打包好的exe文件)

    使用Python第三方库xlwings将单个excel工作表快速拆分为多个工作表(附打包好的exe文件) 资源链接 CSDN:https://download.csdn.net/download/qq ...

  6. 快速找到python第三方库

    在window系统中安装python第三方库的几种方法: 1.百度搜索相应的库名,如numpy,scipy,matplotlib,可以搜索关键词numpy Python Package Index,s ...

  7. Python标准库:turtle库的简单快速入门

    在计算机二级Python的计算生态(其实也就是库)考察中,可以分为两大类:标准库和第三方库. 而标准库中明确要求掌握的就是turtle库和random库,第三方库大都是掌握其在各个常用领域的一些主流库 ...

  8. Python 第三方库大全看这一篇就够了(1000+工具包)

    awesome-python 是 vinta 发起维护的 Python 资源列表,内容包括:Web 框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文本处理.自然语言处理.机器学 ...

  9. 2019 年 10 大顶级 Python 支持库

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 安得广厦千万间,大庇天下寒士俱欢颜 ...

最新文章

  1. c# 垃圾回收是引用类型而言的
  2. python中如何判断两个字符串是否相等_python怎样判断两个字符串是否相同
  3. oracle导出多CSV文件的靠谱的
  4. 购物搜索引擎架构的变与不变——淘宝网曲琳
  5. 【Android 逆向】Android 逆向通用工具开发 ( 网络模块开发 | 配置头文件 | 配置编译参数 | 网络初始化 WSAStartup 与清理 WSACleanup 操作 )
  6. JAVA程序禁用Hbase中的表_HBase禁用表
  7. Android R.mipmap(或drawable或其他).变量
  8. 使用powermock 测试static 方法,jacoco统计覆盖率问题
  9. 前端:收集前端开发者手边必备的11款神器
  10. 【官方活动】亲,咱简历上的精通‘微服务’您真的懂吗? 奉送21天实战微服务免费课程
  11. Cisdem DVD Burner for Mac(DVD光盘刻录软件)
  12. web 开发安全性问题
  13. 【转载】阿里数据技术大图详解
  14. gRPC 流式传输极简入门指南
  15. SSL证书问题SSL certificate problem: self signed certificate
  16. 2022-2027年中国非人寿保险市场竞争态势及行业投资前景预测报告
  17. angularjs实现 - 增删改查+排序+敏感字(最终版)
  18. 用VS实现图片的读取、显示与存储
  19. 如何从后台拿到数据使用elementUi进行导出
  20. threejs 特效 自定义发光围栏,发光墙体,闪烁动画

热门文章

  1. Cobalt Strike-修改默认证书、混淆流量-教程
  2. The server quit without updating PID file解决方法
  3. JDRead电子书使用心得
  4. python安装报_ssl问题
  5. c语言怎么打开mp4格式错误,c语言怎么播放MP4?
  6. springboot controller 中新建一个控制器类 提示该类从未被使用
  7. 华为计算机魔术,华为荣耀magic给大家变了一个魔术,想“拆穿”他吗?
  8. Visual Micro for Microsoft Visual Studio 2022.08
  9. c/c++ 秦九昭算法
  10. 【数字IC手撕代码】Verilog固定优先级仲裁器|题目|原理|设计|仿真