Python 使用requests中文乱码问题

一、首先我们要知道的一个知识点:《HTTP权威指南》里第16章国际化里提到,如果HTTP响应中Content-Type字段没有指定charset,则默认页面是’ISO-8859-1’编码。这处理英文页面当然没有问题,但是中文页面,就会有乱码了!

import requests
# req = requests.get("http://www2.nkfust.edu.tw")
req = requests.get("http://www.baidu.com")
print(req.encoding) # ISO-8859-1

从上面的代码我们可以看得出来,访问百度最终打印的结果为:ISO-8859-1,换个网址会怎么样?结果是一样的!原因正如上面所说:如果HTTP响应中Content-Type字段没有指定charset,则默认页面是’ISO-8859-1’编码。那到底如何处理使用requests过程中乱码的问题呢?接下来主要介绍三种方式:

二、获取请求响应的编码方式:

1、get_encodings_from_content 该函数主要是将响应的内容进行正则从而提取出响应的编码方式,下面我们通过python源码进行说明:

def get_encodings_from_content(content):
    """Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    """
    warnings.warn((
        'In requests 3.0, get_encodings_from_content will be removed. For '
        'more information, please see the discussion on issue #2266. (This'
        ' warning should only appear once.)'),
        DeprecationWarning)

    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content))

可以很清楚的看到,通过正则比配content内容里charset,content,encoding三个字符串后面的数值即编码方式,并已列表的形式返回所匹配到的值。

2、get_encoding_from_headers 该函数主要是通过匹配请求头相应的信息返回响应的编码方式,同样我们从python源码说起:

def get_encoding_from_headers(headers):
    """Returns encodings from given HTTP Header Dict.

    :param headers: dictionary to extract encoding from.
    :rtype: str
    """

    content_type = headers.get('content-type')

    if not content_type:
        return None

    content_type, params = _parse_content_type_header(content_type)

    if 'charset' in params:
        return params['charset'].strip("'\"")

    if 'text' in content_type:
        return 'ISO-8859-1'
        
def _parse_content_type_header(header):
    """Returns content type and parameters from given header

    :param header: string
    :return: tuple containing content type and dictionary of
         parameters
    """

    tokens = header.split(';')
    content_type, params = tokens[0].strip(), tokens[1:]
    params_dict = {}
    items_to_strip = "\"' "

    for param in params:
        param = param.strip()
        if param:
            key, value = param, True
            index_of_equals = param.find("=")
            if index_of_equals != -1:
                key = param[:index_of_equals].strip(items_to_strip)
                value = param[index_of_equals + 1:].strip(items_to_strip)
            params_dict[key.lower()] = value
    return content_type, params_dict

源码内容非常简单,通过获取请求头里content-type来返回最终的编码方式,而content_type, params = _parse_content_type_header(content_type)的作用是将content-type里的内容提取出来,并以字典的形式(params_dict)返回,函数get_encoding_from_headers中判断如果存在键charset则返回对应的编码方式,如果不存在则默认为ISO-8859-1编码方式,这也在一定程度上说明了为什么第一节中req.encoding的打印结果是ISO-8859-1的原因了。

3、最简单的方式当然放到最后面了,直接上代码:

import requests
from lxml import etree
# req = requests.get("http://www2.nkfust.edu.tw")
req = requests.get("http://www.baidu.com")
print(req.apparent_encoding) #utf-8

apparent_encoding是以上三种方式中最为直接,准确获取响应的编码方式的方法,这里不要提再req.encoding最直接了,因为它本身并不准确。

三、获取响应编码方式通用代码:

import requests
req = requests.get("http://www.baidu.com")
if req.encoding == 'ISO-8859-1':
    encodings = requests.utils.get_encodings_from_content(req.text)
    if encodings:
        encoding = encodings[0]
    else:
        encoding = req.apparent_encoding
else:
    encoding = req.encoding
print(encoding)

代码的逻辑很简单,我们首先通过req.encoding判断改HTTP请求,响应是否符合国际标准,如果符合的话默认req.encoding=ISO-8859-1,进入if通过requests.utils.get_encodings_from_content函数获取编码方式,如果有输出结果,没有的话在使用最后一招req.apparent_encoding,直接获取编码方式。当然也有不按国际标准来的另类。

四、有关requests.utils包的拓展:

1.requests.utils.dict_from_cookiejar()将服务器返回的cookie转成字典的形式,代码验证:

import requests
req = requests.get("http://www.baidu.com")
print(req.cookies)  #<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
print(requests.utils.dict_from_cookiejar(req.cookies))  #{'BDORZ': '27315'}

2、requests.utils.add_dict_to_cookiejar()手动添加cookie:

import requests
req = requests.get("http://www.baidu.com")
print(req.cookies) #<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
print(requests.utils.add_dict_to_cookiejar(req.cookies, {"name": "wangcai"})) #<RequestsCookieJar[<Cookie name=wangcai for />, <Cookie BDORZ=27315 for .baidu.com/>]>
print(requests.utils.dict_from_cookiejar(req.cookies)) #{'name': 'wangcai', 'BDORZ': '27315'}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值