python学习之 requests爬虫导致的中文乱码

首先是官方文档

Compliance

Requests is intended to be compliant with all relevant specifications and RFCs where that compliance will not cause difficulties for users. This attention to the specification can lead to some behaviour that may seem unusual to those not familiar with the relevant specification.

Encodings

When you receive a response, Requests makes a guess at the encoding to use for decoding the response when you access the Response.text attribute. Requests will first check for an encoding in the HTTP header, and if none is present, will use chardet to attempt to guess the encoding.

The only time Requests will not do this is if no explicit charset is present in the HTTP headersand the Content-Type header contains text. In this situation, RFC 2616 specifies that the default charset must be ISO-8859-1. Requests follows the specification in this case. If you require a different encoding, you can manually set the Response.encoding property, or use the rawResponse.content.



官方文档的意思就是,如果requests没有发现http headers中的charset,就会使用默认的IOS-8859-1(也就是我们常说的latin-1,但是我们一般的网页使用的charset其实是utf-8)这会导致什么结果呢?


requests获取html文档的时候,获取的实际上是一串字节流(类似python中的str),由于python默认的编码是unicode,所以requests就会尝试把这串字节流decode成unicode编码(关于encode和decode,请参见http://blog.csdn.net/sentimental_dog/article/details/52658725)
而由于目标url的headers没有提供charset,那么这串字节流就会用latin-1 转换为 unicode 编码的方式转换成了我们见到的unicode对象。但是由于网页的编码方式实际上是utf-8,,所以我们实际上需要的是从utf-8转换成unicode编码。 此时这一串字节流就会被错误地解释成unicode编码
 那么我们该如何还原它呢,我们需要先把这个对象encode(latin-1) (恢复原来的字节流),然后再decode('utf-8')(把字节流用utf-8的编码格式解码),这样就获得了utf-8 字节流转换成的unicode对象



我们如何发现这种情况呢?
其实很简单,我们只要知道reponse的encoding方式是否错误就可以了
<span style="font-size:18px;">url = 'http://weather.sina.com.cn/xiamen'
content = requests.get(url)
print content.encoding #ISO-8859-1
#这就说明了编码方式的确是latin-1</span>

当然,如果我们知道网页的编码方式是utf-8,我们可以在调用response.text()之前使用response.encoding='utf-8',这样就不需要像上文一样先使用encoding('latin-1')还原之后再decoding了


requests的原始内容在 response.content 里,是bytes形式自己想怎么处理就怎么处理。
另外,细心的同学可能发现很多时候print response.content和print response.text的结果其实是一样的,这是为什么呢?
根据上文官方文档中的信息,response.content其实提供的是原本的字节流,由于网页是用utf-8编码的,这时候实际上我们就得到了一个 utf-8编码的字符串 ,这时候使用print response.content当然是没有问题的
而response.text所做的,其实是对response.content调用了decode方法,获得了一个unicode对象,如果编码方式是正确的,那么其实我们所做的只是把utf-8编码的字节流转换成了一个unicode对象,当然print的时候是相同的。但是,如果response.encoding不正确的话,这个unicode对象当然就不是我们想要的了,所以会出现错误

关于unicode和utf-8对象的区别:参见:http://blog.csdn.net/sentimental_dog/article/details/52662259
本文参考依云大大在sf的回答https://segmentfault.com/q/1010000000341014


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫中,如果遇到HTML中文乱码问题,可以通过以下几个步骤解决: 1. 首先,可以使用requests库发送HTTP请求获取HTML内容。在请求之后,可以通过response.encoding修改编码方式来解决乱码问题。比如,可以将编码方式设置为'gbk'或'utf-8'。 2. 如果网页的编码为utf-8,但是中文仍然显示乱码,可以尝试进行两次重编码。第一次将编码方式设置为'GBK',然后再将编码方式设置为'utf-8'。 3. 如果以上方法仍然无法解决中文乱码问题,可以尝试通过其他方式来处理。例如,可以使用headers设置User-Agent来模拟浏览器访问网页,或者使用其他第三方库来处理编码问题。 综上所述,解决Python爬虫HTML中文乱码问题的步骤包括修改编码方式、进行重编码以及尝试其他处理方式。根据具体情况选择合适的方法来解决乱码问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python爬虫中文乱码问题](https://blog.csdn.net/wvqusrtg/article/details/113742520)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python爬虫——爬取网页时出现中文乱码问题](https://blog.csdn.net/lucky_shi/article/details/104602013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值