作者IsMe1086 (大頭)
看板Python
標題[問題] 中文編碼
時間Sat Dec 19 09:44:46 2015
小弟是剛接觸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
→ alibuda174: open時也指定編碼utf-8試試 12/19 09:53
→ IsMe1086: 可以了!感謝你~~~ 12/19 10:22