2016-08-17 22 views
0

我解析位於此鏈接的XML,當我訪問一個XML節點的數據:如何使用ElementTree的

XML File to Parse

我需要訪問節點中的數據,它似乎像程序我寫的是告訴我節點內沒有任何東西。這裏是我的代碼:

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' 

我不明白爲什麼它說當我試圖訪問節點的內容時沒有'文本'屬性。

我在做什麼錯?

+0

我添加了XML文件所在的特定鏈接以供測試... – lopezdp

回答

1

幾點意見對你的方法:

for count in comments.findall('count'): 
    print comments.find('count').text 

comments.findall('count')因爲comments沒有名爲count任何直接子元素返回一個空列表。

for child in comments: 
    print child.tag, child.attrib 

遍歷根節點的直接子元素,稱爲note

# From update #2 
for count in comments.findall('comments/comment/count'): 
    print 'count', count.find('count').text 

這裏,count是表示其本身不包含任何count節點count節點Element對象。因此,count.find('count')返回一個NoneType對象。

如果我理解正確,您的目標是檢索count節點的文本值。這裏有兩種方法可以實現:

for count in comments.findall('comments/comment/count'): 
    print count.text 

for comment in comments.iter('comment'): 
    print comment.find('count').text 
+0

感謝您的反饋!這一切都是有道理的,你把它擺出來。給我幾個小時來看看這個結果,如果它出現病倒給你豎起大拇指 – lopezdp

+0

youre the man !!!!! – lopezdp