requests和BeautifulSoup中文编码转换心得

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.encoding
soup = 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')
运行结果:
<title>非言情小说网|同人小说|古代纯爱小说|现代纯爱小说|同人纯爱小说|动漫同人小说【晋江文学城】bl小说站</title>
<title>非言情小说网|同人小说|古代纯爱小说|现代纯爱小说|同人纯爱小说|动漫同人小说【晋江文学城】bl小说站</title>

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

BS_prettify.png


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



  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值