python3请求网页出现乱码

 今天使用python脚本的requests请求一个网页,返回的内容出现了乱码,代码和现象如下图1:

import requests


def do_post():
    r =requests.get("xxxxx")
    if r.status_code != requests.codes.ok:
        return None
    print(r.text)

 上网搜了解决方案,是页面编码问题导致的,在此做个记录。

1.较为规范的网页,都会在返回头中指明charset,即页面的编码。

比如,我们请求"必应"的首页,返回内容指明了页面编码是utf-8。

而有些网站没有返回charset内容,requests模块会默认其编码为"ISO-8859-1"。该编码显示英文页面没有问题,显示中文则乱码。

2.requests提供了几个参数和函数,可用于获取、记录、设置编码。

        r.encoding:记录和设置页面的编码。如返回header里没有charset,则r.encoding = "ISO-8859-1"

        r.apparent_encoding:记录了从返回内容中分析出的响应内容编码方式,猜测可能有些网页内容中指明了编码,如图1的第四行。

        requests.utils.get_encodings_from_content:requests模块提供的一个函数,从body获取页面编码,注意内容必须是str格式。其功能和上面的差不多

        r.content:二进制方式(即bytes方式)的页面响应内容,使用 r.encoding 记录的编码方式进行存储。因为是二进制数据,即一个byte作为一个单位记录信息。

        r.text:字符串方式(即str方式)的页面响应内容,在python3中是unicode编码。

        r.text和r.content我们可以看下requests的源码,如下:

 @property
    def text(self):
        """Content of the response, in unicode.

        If Response.encoding is None, encoding will be guessed using
        ``chardet``.

        The encoding of the response content is determined based solely on HTTP
        headers, following RFC 2616 to the letter. If you can take advantage of
        non-HTTP knowledge to make a better guess at the encoding, you should
        set ``r.encoding`` appropriately before accessing this property.
        """

        # Try charset from content-type
        content = None
        encoding = self.encoding

        if not self.content:
            return str('')

        # Fallback to auto-detected encoding.
        if self.encoding is None:
            encoding = self.apparent_encoding

        # Decode unicode from given encoding.
        try:
            content = str(self.content, encoding, errors='replace')
        except (LookupError, TypeError):
            # A LookupError is raised if the encoding was not found which could
            # indicate a misspelling or similar mistake.
            #
            # A TypeError can be raised if encoding is None
            #
            # So we try blindly encoding.
            content = str(self.content, errors='replace')

        return content

可知在模块中,

  1. 判断 r.encoding 是否有值,有值则以此为转换编码
  2. 若r.encoding为None,则以 r.apparent_encoding 的值设置为转换编码
  3. 根据转换编码,将r.content 转为r.text

注意:python3中默认都是unicode编码,所以unicode编码转为其他编码是encode函数,反之是decode函数。

        r.content和r.text可以通过decode/str函数和encode/bytes函数进行互转,即:

        r.content.decode(r.encoding) == str(r.content,encoding=r.encoding) == r.text

        r.text.encode(r.encoding) ==  bytes(r.text,encoding=r.encoding) ==   r.content

我们可以使用代码做下验证:

import requests


def do_post():
    r =requests.get("xxxxx")
    if r.status_code != requests.codes.ok:
        return None
  
    print("r.encoding:", r.encoding)
    print("r.apparent_encoding:", r.apparent_encoding)
    print("requests.utils.get_encodings_from_content:", requests.utils.get_encodings_from_content(r.text))

    print("content to text 1:", r.content.decode(r.encoding) == r.text)
    print("content to text 2:", str(r.content, encoding=r.encoding) == r.text)

    print("text to content 1:", r.text.encode(r.encoding) == r.content)
    print("text to content 2:", bytes(r.text, encoding=r.encoding) == r.content)


打印结果:

3.综合1和2,得到结论和解决方法。

  1. 请求的页面没有返回charset,所以 r.encoding是默认的编码"ISO-8859-1"。
  2. 实际上,我们从r.apparent_encoding就可以知道,返回的页面是utf-8编码,所以 r.content 的内容是utf-8编码形式的二进制内容
  3. 因为 r.encoding 不对,r.text 对 r.content使用 ISO-8859-1 方式进行解码,得到的肯定是乱码

解决方法:

        将 r.content 根据正确编码进行转换,或者将r.encoding设置为正确的utf-8内容。

import requests


def do_post():
    r =requests.get("xxxxx")
    if r.status_code != requests.codes.ok:
        return None
  
    print("r.encoding:", r.encoding)
    print("r.apparent_encoding:", r.apparent_encoding)
    print("requests.utils.get_encodings_from_content:", requests.utils.get_encodings_from_content(r.text))

    print("content to text 1:", r.content.decode(r.encoding) == r.text)
    print("content to text 2:", str(r.content, encoding=r.encoding) == r.text)

    print("text to content 1:", r.text.encode(r.encoding) == r.content)
    print("text to content 2:", bytes(r.text, encoding=r.encoding) == r.content)


    # 方法1,根据编码进行转换
    resp1 = r.content.decode(r.apparent_encoding)
    # print(r.text)

    # 方法2,设置页面的编码
    r.encoding = r.apparent_encoding
    resp2 = r.text
   
    print("resp1 and resp2:", resp1 == resp2)
    print("++++++++++++++++++++++++++++++++++")
    print("resp:", resp1)

输出结果,不乱码了:

整理了很久,自己也算梳理清楚了。  

ps:之前看到返回页面的 Content-Encoding是gzip,还以为是压缩导致的乱码。其实requests的r.content会自动解码 gzip 和 deflate 压缩。

pps:还有一种unicode-escape编码,是将unicode的内存编码值直接存储,在此只为做个提醒。

参考:python中unicode和unicodeescape - 开飞机的贝塔 - 博客园

当我们使用Python进行get请求,并拿到html响应时,有时候可能会遇到乱码的情况。这种情况通常是由于编码问题造成的。 首先,我们需要确保我们正在使用正确的编码进行解码。大部分网页使用的是UTF-8编码,因此我们可以尝试使用UTF-8进行解码。我们可以使用Python的`requests`库来发送get请求,并使用`response.encoding = 'utf-8'`来设置编码。 如果使用UTF-8解码仍然无法正常显示网页内容,那么可能是网页使用的编码与我们猜测的不同。在这种情况下,我们可以尝试使用`chardet`库来检测网页的编码。`chardet`库可以分析网页的内容,并尝试猜测出正确的编码。我们可以使用`chardet.detect(content)`来检测网页内容的编码,然后再使用该编码进行解码。 另外,有时候网页在返回时没有指定正确的编码,我们可以尝试将返回的html内容转化为Unicode,然后再进行解码。我们可以使用`response.content.decode('unicode_escape')`来将html内容转化为Unicode。 最后,如果上述方法都无法解决问题,那可能是网页本身存在乱码的情况,我们无法完全修复。在这种情况下,我们可以尝试使用BeautifulSoup库来处理网页内容,该库可以自动修复一些网页中的乱码问题。 总结起来,当我们使用Python的get请求拿到html乱码时,可以尝试使用UTF-8编码进行解码,使用chardet库检测网页的编码并使用该编码进行解码,将html内容转化为Unicode进行解码,以及使用BeautifulSoup库处理网页内容。根据具体情况选择相应的方法来解决乱码问题
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值