爬虫requests爬去网页乱码问题

1:res.apparent_encoding

2:res.encoding='utf-8'

r.encoding 从HTTP header中猜测的响应内容编码方式
r.apparent_encoding 从内容中分析出的响应内容编码方式(备选编码方式)

                r.encoding:如果header中不存在charset,则认为编码为ISO-8859-1
                r.apparent_encoding:根据网页内容分析出的编码方式

res.encoding=res.apparent_encoding

                                  https://www.jianshu.com/p/d78982126318

import requests
url = 'http://www.chinanews.com/gn/2018/05-24/8521399.shtml'
res = requests.get(url)
res.content
res.encoding #获取res的编码格式
res.headers #获取Content-Type内容
res.apparent_encoding #获取网页正确的编码格式
#html = res.text# 返回的结果是处理过的Unicode类型的数据
print(res.encoding)#获得网页源码的格式 打印显示 ISO-8859-1
print(res.content){网上一个大神的实验}

努力变得不白
原文地址:https://www.cnblogs.com/leolaosao/p/9085462.html