我在解析特定類型的XML時遇到困難。用Python解析XML忽略部分
XML文件看起來像:
<channels>
<genre type = blah1>
<channel name="Channel 1">
<show>
<title>hello</title>
</show>
</channel>
<channel name="Channel 2">
<show>
<title>hello</title>
</show>
</channel>
</genre>
<genre type="blah2">
<channel name="Channel 3">
<show>
<title>hello</title>
</show>
</channel>
</genre>
</channels>
所以我的問題如下:
channelList = rootElem.find(".//channel[@name]")
howManyChannels = len(channelList)
for x in range(1, howManyChannels):
print x
print rootElem.find(".//channel[@name]["+str(x)+"]").get('name')
for y in rootElem.find(".//channel[@name]["+str(x)+"]"):
print y.findtext('title')
這得到與通道2個,然後錯誤:
Traceback (most recent call last):
File "parse.py", line 17, in <module>
print rootElem.find(".//channel[@name]["+str(x)+"]").get('name')
AttributeError: 'NoneType' object has no attribute 'get'
爲什麼沒有按't代碼:
for y in rootElem.find(".//channel[@name]["+str(x)+"]"):
包含第3個頻道,爲什麼它被隔離,因爲它在另一個流派選項卡中?如何更改代碼以適應此?
我試圖在列表中存儲什麼渠道與什麼顯示。
更新: 我不明白爲什麼
channelList = rootElem.find(".//channel[@name][3]")
產生錯誤連外循環。
url = 'myxmlurl.com/xml.xml'
request = urllib2.Request(url, headers={"Accept" : "application/xml"})
u = urllib2.urlopen(request)
tree = ElementTree.parse(u)
rootElem = tree.getroot()
縮進問題。如果您的代碼沒有正確縮進,我們無法對您的代碼發表評論。 – Marcin
請將代碼格式化爲代碼。 – Marcin