我解析位於此鏈接的XML,當我訪問一個XML節點的數據:如何使用ElementTree的
我需要訪問節點中的數據,它似乎像程序我寫的是告訴我節點內沒有任何東西。這裏是我的代碼:
import urllib
import xml.etree.ElementTree as ET
#prompt for link where xml data resides
#Use this link for testing: http://python-data.dr-chuck.net/comments_42.xml
url = raw_input('Enter URL Link: ')
#open url and prep for parsing
data = urllib.urlopen(url).read()
#read url data and convert to XML Node Tree for parsing
comments = ET.fromstring(data)
#the comment below is part of another approach to the solution
#both approaches are leading me into the same direction
#it appears as if the data inside the node is not being parsed/extracted
#counts = comments.findall('comments/comment/count')
for count in comments.findall('count'):
print comments.find('count').text
當我單獨打印'data'變量時,我得到完整的XML樹。但是,當我嘗試訪問特定節點內的數據時,該節點回來爲空。
我也嘗試打印下列代碼,看看我會得到什麼樣的數據備份:
for child in comments:
print child.tag, child.attrib
我得到的輸出是:
note {} comments {}
我在做什麼錯了,我是什麼失蹤?
錯誤試圖訪問節點的不同循環策略時,我得到的一個是這樣的:
Traceback (most recent call last):
File "xmlextractor.py", line 16, in <module>
print comments.find('count').text
AttributeError: 'NoneType' object has no attribute 'text'
請幫助和感謝!
UPDATE:
伊夫實現了通過etree文檔的蟒蛇,我的做法一直在努力「得到」的節點屬性,而不是節點的內容看。我還沒有找到答案,但我絕對接近!
第二更新:
,所以我嘗試了這樣的代碼:
import urllib
import xml.etree.ElementTree as ET
#prompt for link where xml data resides
#Use this link for testing: http://python-data.dr-chuck.net/comments_42.xml
url = raw_input('Enter URL Link: ')
#open url and prep for parsing
data = urllib.urlopen(url).read()
#read url data and convert to XML Node Tree for parsing
comments = ET.fromstring(data)
counts = comments.findall('comments/comment/count')
print len(counts)
for count in counts:
print 'count', count.find('count').text
從如上所述,當運行此代碼我:
print len(counts)
輸出,我已在50個節點我計數列表,但我仍然得到相同的錯誤:
Traceback (most recent call last):
File "xmlextractor.py", line 18, in <module>
print 'count', count.find('count').text
AttributeError: 'NoneType' object has no attribute 'text'
我不明白爲什麼它說當我試圖訪問節點的內容時沒有'文本'屬性。
我在做什麼錯?
我添加了XML文件所在的特定鏈接以供測試... – lopezdp