您现在的位置是:网站首页> 编程资料编程资料

python新一代网络请求库之python-httpx库操作指南_python_

2023-05-25 382人已围观

简介 python新一代网络请求库之python-httpx库操作指南_python_

一、 概述

1、 简介

HTTPX 是 Python 3 的全功能 HTTP 客户端,它提供同步和异步 API,并支持 HTTP/1.1 和 HTTP/2。

官方文档位置:https://www.python-httpx.org/

该库的特性:

HTTPX 建立在公认的可用性之上requests,并为您提供:

加上requests…的所有标准功能

  • 国际域名和 URL
  • 保持活动和连接池
  • 具有 Cookie 持久性的会话
  • 浏览器式 SSL 验证
  • 基本/摘要认证
  • 优雅的键/值 Cookie
  • 自动减压
  • 自动内容解码
  • Unicode 响应体
  • 多部分文件上传
  • HTTP(S) 代理支持
  • 连接超时
  • 流式下载
  • .netrc 支持
  • 分块请求

安装方式:

pip install httpx # 安装库 pip install httpx[http2] # 获取http2的支持 pip install httpx[brotli] # 包括可选的 brotli 解码器支持 

2、 命令行模式

安装: pip install 'httpx[cli]'

现在允许我们直接从命令行使用 HTTPX…

发送请求…

3、 快速开始

3.1 get请求

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx from fake_useragent import UserAgent headers = { "user-agent": UserAgent().random, } params = { "wd": "python" # 输入百度搜索的内容 } resp = httpx.get("https://www.baidu.com/s", params=params, headers=headers, cookies=None, proxies=None) # 和原来requests的使用方法类似 resp.encoding = resp.charset_encoding # 根据文档的编码还对文档进行编码 print(resp.text) # 获取数据信息 

requests中的参数和httpx中的参数大部分类似

3.2 post请求

3.2.1 表单

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx data = {'key1': 'value1', 'key2': 'value2'} r = httpx.post("https://httpbin.org/post", data=data) print(r.text) 

3.2.2 文件

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx files = {'upload-file': open('a.jpg', 'rb')} # 也可以通过元组来指定数据类型 # files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')} r = httpx.post("https://httpbin.org/post", files=files) print(r.text) 

3.2.3 JSON

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx data = {'integer': 123, 'boolean': True, 'list': ['a', 'b', 'c']} r = httpx.post("https://httpbin.org/post", json=data) print(r.text) 

3.2.4 二进制

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx content = b'Hello, world' r = httpx.post("https://httpbin.org/post", content=content, headers={ "Content-Type": "application/octet-stream", }) print(r.text) 

Content-Type在上传二进制数据时设置自定义标头

常见的媒体格式类型如下:

  • text/html : HTML格式
  • text/plain :纯文本格式
  • text/xml : XML格式
  • image/gif :gif图片格式
  • image/jpeg :jpg图片格式
  • image/png:png图片格式

以application开头的媒体格式类型:

  • application/xhtml+xml :XHTML格式
  • application/xml: XML数据格式
  • application/atom+xml :Atom XML聚合格式
  • application/json: JSON数据格式
  • application/pdf:pdf格式
  • application/msword : Word文档格式
  • application/octet-stream : 二进制流数据(如常见的文件下载)
  • application/x-www-form-urlencoded :
    中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)

另外一种常见的媒体格式是上传文件之时使用的:

  • multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式

3.3 响应处理

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx resp = httpx.request("GET", "https://www.baidu.com") if resp.status_code == httpx.codes.OK: print(resp.text) # 如果请求成功 print(resp.raise_for_status()) # 判断响应是否成功,成功返回None,失败则报错 

3.4 流式响应

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

您可以流式传输响应的二进制内容…

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx with httpx.stream("GET", "https://www.example.com") as r: for data in r.iter_bytes(): # 流式传输响应的二进制内容 # for text in r.iter_text(): # 获取全部的文本内容 # for line in r.iter_lines(): # 逐行获取传输响应的文本内容 # for chunk in r.iter_raw(): # 获取编码前的原始数据 # if r.headers['Content-Length'] < TOO_LONG: # 有条件的加载内容 print(data) 

注意:

如果您以任何这些方式使用流式响应,则response.contentandresponse.text属性将不可用

3.5 cookie

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx # 获取cookie r = httpx.get('https://httpbin.org/cookies/set?chocolate=chip') print(r.cookies['chocolate']) # 获取请求中的cookie # 设置cookie cookies_1 = {"peanut": "butter"} cookies_2 = httpx.Cookies() cookies_2.set('cookie_on_domain', 'hello, there!', domain='httpbin.org') cookies_2.set('cookie_off_domain', 'nope.', domain='example.org') r = httpx.get('http://httpbin.org/cookies', cookies=cookies_2) print(r.json()) 

3.6 重定向

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

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

#!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = "A.L.Kun" __file__ = "demo01.py" __time__ = "2022/9/9 7:55" import httpx r = httpx.get('http://github.com/') print(r.status_code) print(r.history) # 查看重定向的记录 print(r.next_request) # 获取到重定向以后的请求对象 resp = httpx.Client().send(r.next_request) # 对请求对象发送请求 print(resp.text) 

那么,我们可不可以跟踪这个重定向呢?其实是可以的:

您可以使

-六神源码网