汉字在python中出错,中文格式写入文件后出现乱码怎么破?,python乱码,一个很简单的小爬虫程序


一个很简单的小爬虫程序

    for i in L:        content = urllib2.urlopen('http://X.X.X.X/cgi-bin/GetDomainOwnerInfo?domain=%s' %i)        html = content.read()        with open('domain_test.xml','a') as f:            f.write(html)            print html

print 的结果是中文:

<domaininfo strDomain="XXX.com." strOwner="XXX" strDepartment="云平台部" strBusiness="[互联网业务系统 - XXX" strUser="XXX;">

但直接打开xml文本的时候却是乱码:

<domaininfo strDomain="XXX.com." strOwner="XXX" strDepartment="云平台部" strBusiness="[互联网业务系统 - 第三方应用]" StrUser="XXX;">

Windows 7 操作系统,python 2.7

请问一下各位,这个问题如何解决?

import codecsf = codecs.open("domain_test.xml", "w", "utf-8")

在文件开头加上 #coding:utf-8

试试在文件开头加上 # -*- coding: utf-8 -*-

编橙之家文章,

评论关闭