使用python对xml文件进行解析的时候,如果xml文件的头文件是utf-8格式的编码,那么解析是ok的,但如果是其他格式将会出现如下异常:
xml.parsers.expat.ExpatError: unknown encoding
因此,为了保证程序的正常运行,我们需要对读取的文件进行编码处理。
1、首先将读取的字符从原来的编码解析,并编码成utf-8;
2、修改xml的encoding;
代码如下:
import sys
import os
import datetime
import time
import string
from urllib import unquote
import MySQLdb
import xml.parsers.expat
import xml.etree.ElementTree as Etree
import types
import httplib
import urllib2
import urllib
import json
import re
def readDataFromNetwork(url):
req = urllib2.Request(url)
rd = urllib2.urlopen(req)
readData = rd.read()
return readData
# <![CDATA[ http://j.xywy.com/il_sii_27.htm ]]>
def parseXmlStr(_str):
try:
# 将字符串进行解码编码
_str = unquote(_str)
_str = _str.decode('gbk').encode('utf-8')
print _str[0:100]
except Exception,ex:
print 'error'
# 修改xml文件的编码方式
_str = re.sub('gbk', 'utf-8', _str)
xmlDoc = Etree.fromstring(_str)
childList = xmlDoc.getchildren()
for node in childList:
str_value = node.find("display/url").text
if str_value.find('CDATA') != -1:
print 'haha'
输出结果如下:
<?xml version="1.0" encoding="gbk" ?>
<DOCUMENT>
<item>
<key>百日咳</key>
<display>
<title>
<?xml version="1.0" encoding="utf-8" ?>
<DOCUMENT>
<item>
<key>百日咳</key>
<display>