大家爬取网页的时候,应当都遇到过这类情况
当我打印网页源代码的时候文章源自微观生活(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)关于“不再用耽心网页编码的坑了!”的详细内容,希望对大家有所帮助!
评论