이 링크에있는 XML을 구문 분석하고 있습니다.
노드 내부의 데이터에 액세스해야하는데 제가 작성한 프로그램이 노드 내부에 아무것도 없다고 알려주는 것 같습니다. 내 코드는 다음과 같습니다.
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
'데이터'변수 만 출력하면 완전한 XML 트리가 표시됩니다. 그러나 특정 노드 내부의 데이터에 액세스하려고하면 노드가 다시 비워집니다.
또한 다음 코드를 인쇄하여 어떤 데이터를 반환할지 확인했습니다.
for child in comments:
print child.tag, child.attrib
내가 얻은 출력은 다음과 같습니다.
메모 {} 댓글 {}
내가 뭘 잘못하고 있고 무엇을 놓치고 있습니까?
노드에 액세스하는 다른 루핑 전략을 시도 할 때 발생하는 오류 중 하나는 다음과 같습니다.
Traceback (most recent call last):
File "xmlextractor.py", line 16, in <module>
print comments.find('count').text
AttributeError: 'NoneType' object has no attribute 'text'
도와 주셔서 감사합니다 !!!
최신 정보:
나는 파이썬에 대한 etree 문서를 살펴보면서 내 접근 방식이 노드의 내용 대신 노드 속성을 '가져 오기'를 시도하고 있음을 깨달았습니다. 나는 아직도 답을 찾지 못했지만 나는 확실히 더 가깝다 !!!
2 차 업데이트 :
그래서 나는이 코드를 시도했습니다.
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'
노드의 내용에 액세스하려고 할 때 '텍스트'속성이 없다고 말하는 이유를 이해할 수 없습니다.
내가 도대체 뭘 잘못하고있는 겁니까??
접근 방식에 대한 몇 가지 의견 :
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
입니다 Element
나타내는 객체 count
자체가 어떤 포함하지 않는 노드 count
노드. 따라서 객체를 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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다