ipython中直接print r.text會出現輸出結果諸如:['\xe4\xbd\xa0\xe5\xa5\xbd', '\xe5\x93\x88\xe5\x93\x88', '\xe5\x91\xb5\xe5\x91\xb5']
因爲默認的編碼爲utf-8,所以要將中文顯示,需要你需要:
r.text.decode('utf-8').encode('gbk')##先轉碼成unicode,再轉碼成gbk輸出
但,此時你還是會失敗,所以:首先你需要執行:
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
否則會出現:
In [15]: print r.text.decode('utf-8').encode('gbk')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-15-ee68b5387e3d> in <module>()
----> 1 print r.text.decode('utf-8').encode('gbk')
C:\Python27\lib\encodings\utf_8.pyc in decode(input, errors)
14
15 def decode(input, errors='strict'):
---> 16 return codecs.utf_8_decode(input, errors, True)
17
18 class IncrementalEncoder(codecs.IncrementalEncoder):
UnicodeEncodeError: 'ascii' codec can't encode characters in position 77-78: ordinal not in range(128)
幾天,裝scrapy又遇到亂碼坑貨問題了。
先是用easy_install 裝,遇到vc++需要手工安裝,然後又是libssl-dev裝不上,搜了半天win下好像也沒有。然後轉戰pip,結果又遇到如下報錯,無法進行了:
<span style="font-size:18px;">UnicodeEncodeError: 'ascii' codec can't encode character u'\u2588' in position 8: ordinal not in range(128)</span>
然後這樣解決了問題:在python的Lib\site-packages文件夾下新建一個sitecustomize.py,內容爲:
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
這樣系統在python啓動的時候,自行調用該文件,設置系統的默認編碼,而不需要每次都手動的加上解決代碼,一勞永逸。
另外,pypi中很多軟件已經不提供exe了。。只提供tar和whl,whl只能用pip安裝,諸如:pip install xxx.whl easy_install則不行,所以以後就用pip啦!