python爬蟲的中文亂碼問題?

做了一個爬蟲的練習。準備把w3school上的一句話「領先的 Web 技術教程 - 全部免費」給爬下來,並用json的方式存檔。代碼一切都好,也列印了代碼12行的s看到「領先的 Web 技術教程 - 全部免費」給取出來了。可存haha.json的時候發現全是uxxxxx的。如何才能存成可見的中文字元呢?


json.dump([s], f, ensure_ascii=False)


這不是亂碼好不好,這是Unicode,你可以給dump函數加個參數,ensure_ascii,如果你要縮進什麼的,可以用indent參數

json.dump([s],f,ensure_ascii=False)

json.dump([s],f,ensure_ascii=False,indent=4)


對於Python3爬蟲抓取網頁中文出現輸出亂碼

import urllib.request
response = urllib.request.urlopen("http://www.baidu.com")
html = response.read()
print(html)

上面的代碼正常但是運行的時候結果遇到中文會以xe7x99xbexe5xbaxa6xe4xb8x80代替,這是一種byte位元組。

python 3輸出位串,而不是可讀的字元串,需要對其進行轉換

使用str(string[, encoding])對數組進行轉換

str(response.read(),"utf-8")

import urllib.request
response = urllib.request.urlopen("http://www.baidu.com")
html =str(response.read(),"utf-8")
print(html)

http://blog.sina.com.cn/u/3951225433


https://docs.python.org/2/library/json.html#json.dump

If ensure_ascii is True (the default), all non-ASCII characters in the output are escaped with uXXXX sequences, and the result is a strinstance consisting of ASCII characters only. If ensure_ascii is False, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode(as in codecs.getwriter()) this is likely to cause an error.


先要把什麼是亂碼搞清楚。。


前兩天寫爬蟲也遇到了
解決:

json.dumps(data, ensure_ascii=False)

JSON encoder and decoder
https://docs.python.org/3.4/library/json.html


在list裡面放著不會默認解編碼的。
直接輸出元素應該就是中文了。
而且json都是編碼過的。


完整的解決方案:

首先需要設置系統默認的編碼方式如上,再使用ensure_ascii=False


Python就這尿性,各種不方便。要方便的話去用高貴的C#撒。


建議題主先了解一下各種文字編碼

http://blog.csdn.net/fmddlmyy/archive/2007/02/14/1510189.aspx

還有盡量用搜索引擎解決問題


python2亂碼可以這樣解決。

能夠實現用print函數列印的編碼是正常的,同時保存為文件的也正常,需要棄用requests而用urllib2

源代碼如下

import urllib2
import sys
content=urllib2.urlopen(「http://……」).read()
typeEncode = sys.getfilesystemencoding()
html = content.decode(「utf-8″).encode(typeEncode)
soup=BeautifulSoup(html,」lxml」)
print soup.prettify()
fp=open(『login.html』,』w』)
fp.write(content)
fp.close()

具體可以參考這個帖子python中文亂碼解決方案


對於Python中文亂碼的問題,

之前用的Python2.7版本,也能夠解決中文亂碼的問題,但是太麻煩啊,

但是後來改成了Python3.5版本,就解決了。

所以還是建議改成3.5版本。


直接換成urllib2庫就好了


這個問題之前也遇到過。如果題主是通過命令行使用feed export時出現的問題。有兩種解決方案:
一、
scrapy通過調用JsonLinesItemExporte來完成輸出JSON格式。只需要在JsonLinesItemExporte的參數中ensure_ascii=False。
二、
通過pipelines中的類來實現自定義輸出json格式。則需要在json.dumps中加上參數ensure_ascii=False。
-------------------------- 分隔線 ------------------------
其實JsonLinesItemExporte中就是調用了json.dumps來轉換item。


json 是utf-8的吧,什麼叫Unicode


新手一枚...給個抓糗百類似的結果供參考...
代碼片段:
pattern = re.compile(r"&(.*?)&", re.S)
items = re.findall(pattern,content)
print items
for item in items:
print item
輸出:
items:
[u"u3002uff0cu3002uff0cu3002uff1fuff1fuff0cuff01u2026uff01uff01uff01", u"u82b1u94b1u66f0u4e0buff5euff5e", u"u4e8cu7f3au9752u5e74$", u"u53bbu53e3u5df4", u"u5e94u8be5u8d77u4ec0u4e48u6635u79f0u5462", u"u8c01u8bb0u5f97u7231u4f60...", u"u60e6u5ff5amp;u5496u5561", u"u672cu55b5u4e0du54acu4eba", u"u96cfu9e70u8d77u98deu3002", u"u83f1u7396\\\\\\^_^\\\\\\", u"onepieceu7f8eu51ccu683c", u"u74dcu54e5u4f9du65e7u6f47u6d12", u"u4e8cu5e08u5144u5728u6b64.", u"u60f3u62a2u65b0u534eu4e66u5e97", u"u8017u67f3u5b50", u"u59d1u59d1u53c8u8ddfu96d5u8d70u4e86", u"u5723u6c34u6d77u9614u5929u7a7a", u"u8f9bu5fb7u52d2u4e4bu6b87", u"u6211u7231u5403u756au8304u9171", u"u60f3u62a2u65b0u534eu4e66u5e97"]

item:
。,。,。??,!…!!!
花錢曰下~~
二缺青年$
去口巴
應該起什麼昵稱呢
誰記得愛你...
惦念amp;咖啡
本喵不咬人
雛鷹起飛。
菱玖\\\^_^\\\
onepiece美凌格
瓜哥依舊瀟洒
二師兄在此.
想搶新華書店
耗柳子
姑姑又跟雕走了
聖水海闊天空
辛德勒之殤
我愛吃番茄醬
想搶新華書店


推薦閱讀:

如何解決Python selenium在遠程shell下無法連接瀏覽器的問題?
如何在 python 中使用 beautifulsoup4 來抓取標籤中的內容?
按鍵精靈等以GUI介面為基礎的程序在爬蟲界的地位是怎樣的?
用 Scrapy 爬蟲怎麼解決動態網頁的問題?
scrapy的request的meta參數是什麼意思?

TAG:Python | 爬蟲計算機網路 | JSON | 中文亂碼 |