python3.8 brotli brotli.error: BrotliDecompress failed?

python3.8 brotli brotli.error: BrotliDecompress failed?

我的代码 python3.8

brotli=1.0.9

使用请求获取 url,headers 使用 Accept-Encoding="br" 我需要使用 decode br,因为我认为使用 accept-encoding=br 更好

import brotli
import requests 
headers = {}
headers['Accept'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
headers['Accept-Encoding'] = "gzip, deflate, br"
headers['Host'] = "book.douban.com"
headers['Referer'] = "book.douban.com"
headers['Sec-Fetch-Dest'] = "document"
headers['Sec-Fetch-Mode'] = "navigate"
headers['Upgrade-Insecure-Requests'] = "1"

s=requests.Session()
url="https://book.douban.com/tag/%E5%B0%8F%E8%AF%B4"
try:
    response = s.get(url, headers=headers)
except:
    return ""
if response.status_code == 200:
    print(response.headers)
    if response.headers.get('Content-Encoding') == 'br':
        data = brotli.decompress(response.content)
        data1 = data.decode('utf-8')
        return data1
    else:
        return response.text
return ""

引发错误

data = brotli.decompress(response.content)
brotli.error: BrotliDecompress failed

安装requests but once brotli的文档中没有提到这个,直接由Requests处理。

这意味着 response.content 将被自动解码(类似于 gzip)。你不需要做 brotli.decompress(response.content)

如果没有安装 brotli,您将不会收到任何错误消息。相反,response.content 将保持编码状态...

编辑:

深入研究 Requests 代码,我发现 Requests 使用 urllib3.response 来实现 Brotli 的用法。

加载后,urllib3.response 将查找 Brotli 的导入:

try:
    import brotli
except ImportError:
    brotli = None

然后在解码响应时,它将使用适当的解码器:

def _get_decoder(mode):
    if "," in mode:
        return MultiDecoder(mode)

    if mode == "gzip":
        return GzipDecoder()

    if brotli is not None and mode == "br":
        return BrotliDecoder()

    return DeflateDecoder()

因此,如果安装了 Brotli,解码将会发生,否则什么也不会发生,也不会向用户发出警告。

edit2 其实这个在https://docs.python-requests.org/en/latest/user/quickstart/#binary-response-content

中有提到