如何解决用 Beautiful Soup 抓取网页却得到乱码的问题?

[title]解决方案[/title]

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
    'Cookie': ''
}
url = 'http://www.weather.com.cn/textFC/hb.shtml'
res = requests.get(url, headers = headers)
# 下面的 res.text 改为 res.content 即可
soup = BeautifulSoup(res.text, 'html.parser')

改为 res.content 即可
soup = BeautifulSoup(res.content, ‘html.parser’)

 

参考文献:https://www.zhihu.com/question/19696249

作者: 高志远

高志远,24岁,男生

发表评论

邮箱地址不会被公开。