python爬虫问题: requests库中文编码问题

为什么会有ISO-8859-1这样的字符集编码

    requests会从服务器返回的响应头的 Content-Type 去获取字符集编码,如果content-type有charset字段那么requests才能正确识别编码,否则就使用默认的 ISO-8859-1. 一般那些不规范的页面往往有这样的问题.

\requests\utils.py

python爬虫问题: requests库中文编码问题

如何获取正确的编码

     requests的返回结果对象里有个apparent_encoding函数, apparent_encoding通过调用chardet.detect()来识别文本编码. 但是需要注意的是,这有些消耗计算资源.

 \requests\models.py

@property

def apparent_encoding(self):

    """The apparent encoding, provided by the chardet library."""

    return chardet.detect(self.content)['encoding']

requests的text() 跟 content() 有什么区别?

    requests在获取网络资源后,我们可以通过两种模式查看内容。 一个是r.text,另一个是r.content,那他们之间有什么区别呢?

分析requests的源代码发现,r.text返回的是处理过的Unicode型的数据,而使用r.content返回的是bytes型的原始数据。也就是说,r.content相对于r.text来说节省了计算资源,r.content是把内容bytes返回. 而r.text是decode成Unicode. 如果headers没有charset字符集的化,text()会调用chardet来计算字符集,这又是消耗cpu的事情.

通过看requests代码来分析text() content()的区别.

python爬虫问题: requests库中文编码问题

 

python爬虫问题: requests库中文编码问题

requests中文乱码解决方法

方法一: 直接encode成utf-8格式.

1r.content.decode(r.encoding).encode('utf-8')

1r.encoding = 'utf-8'

方法二:如果headers头部没有charset,那么就从html的meta中抽取.