python2.7 vs python3.4 抓出來的網頁編碼怎處裏

1,423 views
Skip to first unread message

張建凱

unread,
Dec 1, 2014, 10:51:52 AM12/1/14
to pyth...@googlegroups.com
2.7
import urllib2  
response = urllib2.urlopen('https://www.google.com.tw/')  
html = response.read()  
print html
3.1
import urllib.request
response = urllib.request.urlopen('https://www.google.com.tw/')
html = response.read()
print(html) 

2.7print出來 會顯示中文內容
3.4print出來 \xbby\xa8\xa5\xa4u\xa8\xe3

但是將3.4 encode('utf-8')
錯誤UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb7 in position 4239: invalid start byte

請指點一下

Keith Yang

unread,
Dec 1, 2014, 10:59:04 PM12/1/14
to pyth...@googlegroups.com, pyth...@googlegroups.com
隨手試了一下 3.4,試試: 

type(html)  # 在 2.7 與 3.4 的不同
print(html.decode(‘big5’))  # 用猜出來的

要科學一點的話,可以試 chardet 這套件 (pip install chardet)

>>> import chardet
>>> chardet.detect(html)
 {'encoding': 'Big5', 'confidence': 0.99}


# Keith ")
Sent from Mailbox


--
這是 Google 網上論壇針對「python.tw」群組發送的訂閱通知郵件。
如要取消訂閱這個群組並停止接收來自這個群組的郵件,請傳送電子郵件到 pythontw+u...@googlegroups.com
如要在這個群組張貼留言,請傳送電子郵件到 pyth...@googlegroups.com
請前往以下網址造訪這個群組:http://groups.google.com/group/pythontw
如需更多選項,請前往:https://groups.google.com/d/optout

張建凱

unread,
Dec 2, 2014, 4:05:51 AM12/2/14
to pyth...@googlegroups.com
我有裝套件了
但是若網頁中有© 會發生錯誤 無法print 也無法寫成txt

He-chien Tsai

unread,
Dec 3, 2014, 5:42:04 AM12/3/14
to pyth...@googlegroups.com
建議把urllib2換掉, 改用requests包, 這個比較像是人在用的東西
畢竟現在軍中管理都人性化了, 寫程式也是要人性化
python-requests.org
可以很容易就調整傳進來的編碼
http://docs.python-requests.org/en/latest/user/quickstart/#response-content

Windows有可能是命令終端的問題, Python本身是沒問題的, 但傳到命令終端的時候
命令終端還是用錯誤的編碼解讀, 建議在Windows不要用終端來看輸出編碼是否正確, 也不要去想能不能把命令終端的行為調過來, 亂調調爛了就全部救不回來
建議是直接輸出成純文字檔, 再用文字檢視器, 把檢視器顯示編碼調成適當的編碼再開啟, 這樣比較不用傷筋動骨

張建凱

unread,
Dec 3, 2014, 8:46:16 AM12/3/14
to pyth...@googlegroups.com
今天測試真的是windows 命令終端機問題
我也依照您的建議寫入文字檔結果還是發生
UnicodeEncodeError: 'cp950' codec can't encode character '\xa9'  錯誤

You-Ruei Tzeng

unread,
Dec 3, 2014, 8:51:34 AM12/3/14
to pyth...@googlegroups.com
基本上這個問題大概無解了  因為 cmd 他是 cp950 編碼的  我有測試過就算改 utf8 也是會有問題
你可以用 chcp 指令來更改

Keith Yang

unread,
Dec 3, 2014, 9:02:13 AM12/3/14
to pyth...@googlegroups.com, pyth...@googlegroups.com
好奇有程式碼可以貼到 http://paste2.org/ 供參考嗎?

# Keith ")
Sent from Mailbox


張建凱

unread,
Dec 3, 2014, 10:15:55 AM12/3/14
to pyth...@googlegroups.com
我也在mac下做一次
UnicodeEncodeError: 'ascii' codec can't encode characters in position 84-123

張建凱

unread,
Dec 3, 2014, 10:18:23 AM12/3/14
to pyth...@googlegroups.com
import urllib.request
request = urllib.request.Request("http://www.htc.com/tw/")
request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
htmlCode = urllib.request.urlopen(request).read().decode('utf-8')
txtfile=open('code.txt','w')
txtfile.write(htmlCode)
txtfile.close()

print(htmlCode)
print("end")


Keith Yang於 2014年12月3日星期三UTC+8下午10時02分13秒寫道:

Keith Yang

unread,
Dec 3, 2014, 10:26:08 AM12/3/14
to pyth...@googlegroups.com
Hmmm… 我的 Mac 跑沒問題。

試下指令:
% python -c 'import sys; print(sys.getdefaultencoding())'
utf-8

# Keith ")
Sent from Mailbox


He-chien Tsai

unread,
Dec 3, 2014, 11:38:45 AM12/3/14
to pyth...@googlegroups.com
在python 2不要直接用open
用codecs裡的open並指定編碼
像是
import codecs
f = codecs.open('file.txt', 'w', 'utf8')

Python 3的open 已經取代python 2的codecs.open而且預設編碼也換成utf8
所以在Python 3原本程式碼原封不動可以跑

張建凱

unread,
Dec 3, 2014, 11:58:09 AM12/3/14
to pyth...@googlegroups.com
我是用python3 
不管用哪種方式執行(python idle  cmd )(win mac)
遇到那字元就是會出錯 無法寫入txt



He-chien Tsai於 2014年12月4日星期四UTC+8上午12時38分45秒寫道:

He-chien Tsai

unread,
Dec 3, 2014, 12:10:09 PM12/3/14
to pyth...@googlegroups.com
 @@
我這裡Linux是很順利的執行過去了, 完全測試不出是啥問題
若只是一兩個字元的輸出沒辦法編碼的話, 是可以用這方法跳過
txtfile=open('code.txt','w','utf8', errors='ignore')
只是Linux上不用丟errors='ignore', 就能正常輸出, 而且輸出後還正常顯示
猜是windows系統沒辦法編碼的關係, 即使指定的編碼正確, windows不知道缺了啥沒法編碼
順便講decode跟encode函數也都有errors參數
Reply all
Reply to author
Forward
0 new messages