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'
コードがそうでない理由:
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()