Python parse.xml,前缀为';标签上有什么?xml.etree.ElementTree

Python parse.xml,前缀为';标签上有什么?xml.etree.ElementTree,python,xml,xml.etree,Python,Xml,Xml.etree,我可以读取标签,除非有前缀。我没有运气去搜索前面的问题 我需要阅读媒体:内容。我尝试了image=node.find(“媒体:内容”)。 Rss输入: <channel> <title>Popular Photography in the last 1 week</title> <item> <title>foo</title> <media:category label="Miscellan

我可以读取标签,除非有前缀。我没有运气去搜索前面的问题

我需要阅读
媒体:内容
。我尝试了
image=node.find(“媒体:内容”)
。 Rss输入:

<channel>
  <title>Popular  Photography in the last 1 week</title>
  <item>
    <title>foo</title>
    <media:category label="Miscellaneous">photography/misc</media:category>
    <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/>
  </item>
  <item> ... </item>
</channel>

我一直在使用文档,但仍然停留在“前缀”部分。

media
是一个XML名称空间,它必须在前面的某个地方用
xmlns:media=“…”定义。有关如何在lxml中定义用于XPath表达式的xml命名空间的信息,请参见。

以下是将xml命名空间与ElementTree一起使用的示例:

>x=''\
过去一周的流行摄影
福
摄影/杂项
... 
'''
>>>node=ElementTree.fromstring(x)
>>>对于node.findall('项)中的元素/{http://www.w3.org/TR/html4/}类别“):
打印元素文本
摄影/杂项
from xml.etree import ElementTree
with open('cache1.rss', 'rt') as f:
    tree = ElementTree.parse(f)

for node in tree.findall('.//channel/item'):
    title =  node.find("title").text 
>>> x = '''\
<channel xmlns:media="http://www.w3.org/TR/html4/">
  <title>Popular  Photography in the last 1 week</title>
  <item>
    <title>foo</title>
    <media:category label="Miscellaneous">photography/misc</media:category>
    <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/>
  </item>
  <item> ... </item>
</channel>
'''
>>> node = ElementTree.fromstring(x)
>>> for elem in node.findall('item/{http://www.w3.org/TR/html4/}category'):
        print elem.text


photography/misc