答:
原因: 编码的问题, 因为没有指定编码,默认的编码是 ISO-8859-1, 所以中文显示乱码,解决方案, 见代码
import requests
url = 'https://www.baidu.com/s?%27'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
data = {'wd':'北京'}
#url 请求资源路径
#params 参数
#kwargs 字典
response = requests.get(url=url,params=data,headers=headers)
# 方式一
content = response.content.decode('utf-8')
# print(content)
# 方式 2 修改默认的编码 为 utf-8
# 查看默认的编码
print(response.encoding)
response.encoding = 'utf-8'
print(response.text)