正在做关于爬虫的事情,需要在爬取多个不同网站的新闻网页,一个很重要的问题就到编码问题,爬取到的内容,默认是utf-8编码,但中文网页中常用到'gb2132'或’gbk'编码,如此爬取到的正文就会是乱码。
解决:先提取网页本身的编码,再按照该编码去解码,即可
import chardet
from bs4 import BeautifulSoup
import requests
#使用requests爬虫
def crawler(url):
html = requests.get(url, headers=headers)
#查看网页本身的编码
#html.apparent_encoding或chardet.detect(html.content)['encoding']
#按照当前网页编码方式解码
html.encoding = chardet.detect(html.content)['encoding']
soup = BeautifulSoup(html.text, 'lxml')
##其他......
注:当遇到‘gbk’编码的网页时,可能会有的字无法解码,如爬取到的内容以“\u3000"出现,属于正常情况