不再用耽心网页编码的坑了!

小微 科技不再用耽心网页编码的坑了!已关闭评论152字数 462阅读模式
摘要大家爬取网页的时候,应该都遇到过这种情况当我打印网页源代码的时候发现 全部是乱码的那这个时候应该怎么办呢?requests是如何判断编码首先,response.content返回的...

大家爬取网页的时候,应当都遇到过这类情况

当我打印网页源代码的时候文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

发现 全体是乱码的文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

那这个时候应当如何办呢?文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

requests是怎么判断编码文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

首先,response.content返回的内容 是二进制内容文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

response.text 则是依据设置的encoding来解码文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

Fallback to auto-detected encoding.
if self.encoding is None:
encoding = self.apparent_encoding文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

编码是通过chardet.detect来获取的,文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

def apparent_encoding:
\"\"\"The apparent encoding, provided by the chardet library.\"\"\"
return chardet.detect[\'encoding\']

那么chardet.detect 又是干吗的呢?文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

简单的讲,就是依据给定的字节,来返回他的编码文章源自微观生活(93wg.com)微观生活-https://93wg.com/9366.html

至于他是怎么实现的,

完...

以上就是微观生活(93wg.com)关于“不再用耽心网页编码的坑了!”的详细内容,希望对大家有所帮助!

 
小微
  • 版权声明: 本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们(管理员邮箱:81118366@qq.com),情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!
  • 转载请务必保留本文链接:https://93wg.com/9366.html