我有以下代码来解析 XML,但它不会让我遍历孩子:
import urllib, urllib2, re, time, os
import xml.etree.ElementTree as ET
def wgetUrl(target):
try:
req = urllib2.Request(target)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
outtxt = response.read()
response.close()
except:
return ''
return outtxt
newUrl = 'http://feeds.rasset.ie/rteavgen/player/playlist?showId=10056467'
data = wgetUrl(newUrl)
tree = ET.fromstring(data)
#tree = ET.parse(data)
for elem in tree.iter('entry'):
print elem.tag, elem.attrib
现在,如果我从迭代器中删除“条目”,我会得到这样的输出(为什么是 URL??):
{http://www.w3.org/2005/Atom}entry {}
{http://www.w3.org/2005/Atom}id {}
{http://www.w3.org/2005/Atom}published {}
{http://www.w3.org/2005/Atom}updated {}
{http://www.w3.org/2005/Atom}title {'type': 'text'}
但是,如果我这样放置 iter 语句,它仍然找不到要输入的子项:
for elem in tree.iter('{http://www.w3.org/2005/Atom}entry'):
print elem.tag, elem.attrib
我仍然只得到它自己的入口元素,而不是孩子:
{http://www.w3.org/2005/Atom}entry {}
知道我做错了什么吗?
我到处搜索,但无法弄清楚......我对这一切都很陌生,如果这是愚蠢的事情,我很抱歉。