[問題] 中文編碼

看板Python作者 (大頭)時間10年前 (2015/12/19 09:44), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串2/3 (看更多)
小弟是剛接觸PYTHON的新手 正在練習抓網頁的寫法 已經爬版和GOOGLE 還是找不到方法 先附上程式碼 import urllib import urllib.request thisurl = "http://www.cna.com.tw/" handle = urllib.request.urlopen(thisurl) html_gunk = handle.read().decode(encoding="utf-8",errors="ignore") f = open('t.txt','w') f.write(html_gunk) 然後出現錯誤訊息 Traceback (most recent call last): File "openurl - wf.py", line 15, in <module> f.write(html_gunk) UnicodeEncodeError: 'cp950' codec can't encode character '\xf1' in position 15378: illegal multibyte sequence 麻煩高手指點迷津 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.72.212.247 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1450489489.A.B2A.html

12/19 09:53, , 1F
open時也指定編碼utf-8試試
12/19 09:53, 1F

12/19 10:22, , 2F
可以了!感謝你~~~
12/19 10:22, 2F
文章代碼(AID): #1MTBQHig (Python)
討論串 (同標題文章)
文章代碼(AID): #1MTBQHig (Python)