关于用python把中文字符写入TXT文件的问题
掌上鹅 2017-11-20 06:28:51 刚学python爬虫,于是试着爬一个网页的文本内容,然后想它存入一个TXT文件,然后呢!就是出现了这个错误。
求大佬解答。
#_*_conding=utf-8_*_
import urllib
import urllib2
import re
url = 'http://news.xinhuanet.com/rwk/2013-02/06/c_124330585.htm'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent': user_agent}
try:
request = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(request)
content = response.read().decode('utf-8')
pattern = re.compile(r'<p>(.*?)<\/p>')
items = re.findall(pattern, content)
fl = open('1.txt', 'a')
for line in items:
fl.write(line)
fl.write("\n")
f1.close()
except urllib2.URLError, e:
if hasattr(e, "code"):
print e.code
if hasattr(e, "reason"):
print e.reason
报错代码:UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-11: ordinal not in range(128)