网站爬取时出现乱码-强行转码第二招

如果上一篇文章你依然是乱码,这里我们可以强行指定你的编码#

# 这里因为乱码太强. 只能先get 再去执行 encoding

    # 强行转码第二招

    doc = get('http://localhost/tiyu/详情页/马拉松跑全程是多少千米(公里)_马拉松跑全程大约要用多长时间%20-阳光直播吧.html','')

    # 强行指定返回的数据的类型

    doc.encoding = 'GB2312' 

    # 然后再进行 beautiful 即可中文

    doc = BeautifulSoup(doc.text, 'lxml')

    print('doc: ', doc)

    if doc is None:

        return '请求当前页面是空'+ url

ps: 注意,强行指定,你可以用下面的方法,来看看你自己识别的是什么字符集

print(resp.encoding) #查看网页返回的字符集类型 这里返回ISO-8859 
print(resp.apparent_encoding) #自动判断字符集类型 发现这里返回 GB2312! 我们进行转换gbk 

分析#

requests 和 BeautifulSoup 中文编码转换#

最近在自学用 python 进行网页数据抓取,结果被中文乱码的问题折腾了好久。网上 google 了各种解决方案都无法解决我遇到的问题,索性自己深入的研究了下,终于把这难题给解决了。在此梳理下整个分析过程。

遇到的问题#

一开始我的代码是这样写的:

response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')soup = bs4.BeautifulSoup(response.text, "html.parser")print soup.title

执行后返回的结果中文部分都变成了乱码。很显然,是中文编码在转换过程中出现了问题。

分析#

查看 jjwxc.net 的源码,可以得知该网站的中文编码是 gb18030

<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />

查阅 requestes 以及 BeautifulSoup 的相关文档,可知 requests 会自动将从服务器端获取到的内容自动转换成 unicode, 而 beauifulsoup 也会将获取到内容自动解码成 unicode。既然 response.text 已经是 unicode 形式,那么再传递给 beautifulsoup,是 unicode->unicode 之间的直接传递,应该不存在编码转换错误的情况,那么为什么最后 print 出来的会是乱码呢?

于是进一步查阅 requests 和 bs4 的官方文档,发现了这样两段描述:

When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text. You can find out what encoding Requests is using, and change it, using the r.encoding property.

Beautiful Soup uses a sub-library called Unicode, Dammit to detect a document’s encoding and convert it to Unicode. The autodetected encoding is available as the .original_encoding attribute of the BeautifulSoup object.Unicode, Dammit guesses correctly most of the time, but sometimes it makes mistakes. Sometimes it guesses correctly, but only after a byte-by-byte search of the document that takes a very long time. If you happen to know a document’s encoding ahead of time, you can avoid mistakes and delays by passing it to the BeautifulSoup constructor as from_encoding.

大意是 requests 和 beautifulsoup 都会自行猜测原文的编码方式,然后用猜测出来的编码方式进行解码转换成 unicode。大多数时候猜测出来的编码都是正确的,但也有猜错的情况,如果猜错了可以指定原文的编码。
OK,那让我们看一下 requests 和 beautifulsoup 是否猜对了原文编码。

response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')print response.encodingsoup = bs4.BeautifulSoup(response.text, "html.parser")print soup.original_encoding 运行结果:ISO-8859-1None

可以发现是由于 requests 这里对原文的编码猜错了导致乱码的出现,所以我们需要在 response.text 传给 beautifulsoup 之前指定编码:

response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')response.encoding = 'gb18030'soup = bs4.BeautifulSoup(response.text, "html.parser")print soup.title

但是运行后发现输出的结果还是乱码。
继续查阅上述语句的相关官方文档,发现 beautifulsoup 对于输出内容的编码方式有这样一段介绍:

BS_encoding.png

意即 beautifulsoup 在输出文本时默认以 UTF-8 的方式编码,无论原文是否以它进行编码的。如果你不希望以 UTF-8 的方式编码,可以用 prettify()或则 encode()方式来指定编码。
所以我们将代码更改下:

response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')response.encoding = 'gb18030'soup = bs4.BeautifulSoup(response.text, "html.parser")print soup.title.prettify('gb18030')print soup.title.encode('gb18030') 运行结果:

可以看到两种指定方式都可以获得无乱码的中文内容。
这里再介绍下 prettify () 的功能,prettify () 除了可以制定输出的编码方式,它的最主要功能是对 beautifulsoup 的 k 语法分析树重新排版,使输出的内容整洁易读。这里用一段代码说明:

BS_prettify.png

至此,中文乱码的问题解决了。在查找解决方案的过程中,我另外查了下 unicode、byte、以及编码和解码方面的知识点。这一块的东西解释起来一时半会儿说不完,而且有些细节的地方我也没完全搞懂,暂时就不卖弄了。对这块内容感兴趣的可以先看下下面这两位大牛的文章,写得非常通俗易懂:

参考链接:#

blog.csdn.net/he_and/article/detai...

本作品采用《CC 协议》,转载必须注明作者和本文链接
・15 年技术深耕:理论扎实 + 实战丰富,教学经验让复杂技术变简单 ・8 年企业历练:不仅懂技术,更懂业务落地与项目实操 ・全栈服务力:技术培训 | 软件定制开发 | AI 智能化升级 关注「上海 PHP 自学中心」获取实战干货
wangchunbo
司机 @ 某医疗行业
文章
315
粉丝
353
喜欢
567
收藏
1138
排名:60
访问:12.6 万
私信
所有博文
社区赞助商