from xml.dom.minidom import parseString, parse
dom = parse('response.xml')
xmlTag = dom.getElementsByTagName('link')[0].toxml()
如何获取 rel="alternate"的 xml 属性“href”:
<link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=gfyKPbic&feature=youtube_gdata'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/gf7zhyKPbic/responses'/>
最佳答案
DOC = """<root>
<link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=gfyKPbic&feature=youtube_gdata'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/gf7zhyKPbic/responses'/>
</root>"""
from xml.dom.minidom import parseString, parse
dom = parseString(DOC)
hreflist= [elt.getAttribute("href") for elt in dom.getElementsByTagName('link') if elt.getAttribute("rel")=="alternate"]
for href in hreflist:
print(href)
关于python - 在Python中,检索Xml属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8240849/