python 输出结果乱码解决方法——res.encoding=‘utf-8‘

Python中文乱码的原因,Python中文乱码是由于Python在解析网页时默认用Unicode去解析,而大多数网站是utf-8格式的,并且解析

出来之后,python竟然再以Unicode字符格式输出,会与系统编码格式不同,导致中文输出乱码。

import requests

import sys

from bs4 import BeautifulSoup

url = 'https://www.niceloo.com/cjkj/news_gl/168085.html'

res = requests.get (url)

#res.encoding='utf-8'

print(res.status_code)

soup = BeautifulSoup(res.text,'html.parser')

items = soup.find_all(class_='dert_con')

print(items)

获取响应结果后就设置编码为utf-8

 我们在第6行添加上代码 res.encoding='utf-8'  设置编码后,便不再出现乱码 即对获取的响应结果设置编码

 

来源:敏姐的后花园

物联沃分享整理
物联沃-IOTWORD物联网 » python 输出结果乱码解决方法——res.encoding=‘utf-8‘

发表评论