[问题] 中文编码

楼主: IsMe1086 (大头)   2015-12-19 09:44:46
小弟是刚接触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
麻烦高手指点迷津
作者: alibuda174 (阿哩不达)   2015-12-19 09:53:00
open时也指定编码utf-8试试
楼主: IsMe1086 (大头)   2015-12-19 10:22:00
可以了!感谢你~~~

Links booklink

Contact Us: admin [ a t ] ucptt.com