python - 在Python中,检索Xml属性值

标签 python xml

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&amp;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&amp;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/

相关文章:

java - 找不到类错误膨胀类 android.support.design.widget.BottomNavigationView

sql - 使用 SQL 解析 XML

python - 类型错误 : 'NoneType' object is not subscriptable followed by AttributeError: 'NoneType' object has no attribute 'split'

python - 为什么使用 sudo pip 是一个坏主意?

python - 使用 sox 在 Django 表单类中修剪声音文件

c# - 托管在 Windows 服务中的 WCF 服务 - 错误/预期的命名空间

python - 粘贴程序无法停止守护进程

python - cypher 中灯泡 neo4j 的嵌入和删除

C# 应用程序在关闭自定义表单时崩溃

c# - 如何在 C# (System.Xml.Serialization) 中反序列化 XML 命名空间?