b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

requests和BeautifulSoup中文编码转换心得

电脑杂谈  发布时间:2019-09-05 05:04:11  来源:网络整理

beautifulsoup乱码_linux中文乱码_beautifulsoup中文乱码

最近在自学用python进行网站数据读取,结果被英文乱码的问题折腾了很久。网上google了诸多解决方案都能够缓解我见到的难题,索性自己深入的研究了下,终于把这问题给解决了。在此梳理下整个剖析过程。

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

<pre><code>response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')

soup = bs4.BeautifulSoup(response.text, "html.parser")

print soup.title

</code></pre>

执行后返回的结果英文部分都成为了乱码。很显然,是英文编码在转化过程中发生了难题。

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

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

beautifulsoup中文乱码_linux中文乱码_beautifulsoup乱码

查阅或者BeautifulSoup的相关文档,可知requests会手动将从服务器端获取到的内容手动转化成unicode, 而beauifulsoup也会将获得到内容手动解码成unicode。既然response.text已经是unicode方式,那么再传递给beautifulsoup,是unicode->unicode之间的直接释放beautifulsoup中文乱码,应该不存在编码转换错误的状况,那么为什么最终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是否猜对了原文编码。

<pre><code>response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')

print response.encoding

soup = bs4.BeautifulSoup(response.text, "html.parser")

beautifulsoup中文乱码_beautifulsoup乱码_linux中文乱码

print soup.original_encoding

<br />运行结果:

ISO-8859-1

None</code></pre>

可以看到是因为requests这里对原文的编码猜错了造成乱码的发生beautifulsoup中文乱码,所以我们应该在response.text传给beautifulsoup之前指定编码:

<pre><code>response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')

response.encoding = 'gb18030'

soup = bs4.BeautifulSoup(response.text, "html.parser")

print soup.title</code></pre>

beautifulsoup中文乱码_linux中文乱码_beautifulsoup乱码

但是运行后看到输出的结果而是乱码。

继续查阅上述段落的相关官网文档,发现beautifulsoup对于输出内容的编码方法有这么一段介绍:

BS_encoding.png

意即beautifulsoup在输出文本时默认以UTF-8的方法编码,无论原文是否以它进行编码的。如果你不期望以UTF-8的方法编码,可以用prettify()或则encode()方式来指定编码。

所以我们将代码修改下:

<pre><code>response = requests.get('http://www.jjwxc.net/fenzhan/noyq/')

response.encoding = 'gb18030'

soup = bs4.BeautifulSoup(response.text, "html.parser")

linux中文乱码_beautifulsoup中文乱码_beautifulsoup乱码

print soup.title.prettify('gb18030')

print soup.title.encode('gb18030')

<br />运行结果:

<title>

非言情小说网|同人|古代言情电影|现代悬疑小说|同人言情|同人电影【晋江文学城】bl小说站

</title>

<title>非言情小说网|同人|古代言情电影|现代悬疑小说|同人言情|同人电影【晋江文学城】bl小说站</title></code></pre>

可以发现两种指定模式都可以获得无乱码的英文内容。

这里再介绍下prettify()的功能,prettify()除了可以建立输出的编码方法,它的最主要功能是对beautifulsoup的k语法分析树再次排版,使输出的内容整洁易读。这里用一段代码说明:

BS_prettify.png

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


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-121756-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...