Python 为什么在django中使用lxml.etree.parse和tree.xpath没有得到任何结果?

Python 为什么在django中使用lxml.etree.parse和tree.xpath没有得到任何结果?,python,xml,django,json,parsing,Python,Xml,Django,Json,Parsing,我试图在django中使用lxml.etree.parse和tree.xpath来解析外部rss提要中的一些内容。但由于某种原因,我无法得到任何结果。我以前能够在其他xml文件上成功地使用下面的方法,但这一方法似乎有困难 下面是我试图从中提取的xml文件的外观: <feed xmlns="http://www.w3.org/2005/Atom"> <title>Open Library : Author Name</title> <lin

我试图在django中使用
lxml.etree.parse
tree.xpath
来解析外部rss提要中的一些内容。但由于某种原因,我无法得到任何结果。我以前能够在其他xml文件上成功地使用下面的方法,但这一方法似乎有困难

下面是我试图从中提取的xml文件的外观:

<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Open Library : Author Name</title>
    <link href="http://www.somedomain.org/people/atom/author_name" rel="self"/>
    <updated>2012-03-20T16:41:00Z</updated>
    <author>
        <name>somedomain.org</name>
    </author>
    <id>tag:somedomain.org,2007:/person_feed/123456</id>
    <entry>
        <link href="http://www.somedomain.org/roll_call/show/1234" rel="alternate"/>
        <id>
        tag:somedomain.org,2012-03-20:/roll_call_vote/1234
        </id>
        <updated>2012-03-20T16:41:00Z</updated>
        <title>Once upon a time</title>
        <content type="html">
        This os a book full of words
        </content>
    </entry>
</feed>
我还尝试了以下一些方法,希望它们能有所帮助,但没有成功

    listings = tree.xpath("feed/author")
    listings = tree.xpath("/feed/author")
    listings = tree.xpath("/author")
    listings = tree.xpath("author")

如果有任何方向正确的帮助,我们将不胜感激。

也许问题在于名称空间。lxml模块在标记名的开头加上名称空间名称的前缀,因此可能问题在于xpath表达式与该名称空间前缀不匹配。如果遍历元素查看标记名,得到如下结果,则问题在于:

>>> for element in tree:
...     element
[...]
<Element {http://www.w3.org/2005/Atom}author at 7f14e75d1788>
[...]
树中元素的
>>:
...     要素
[...]
[...]
检查标记名“author”前面的前缀“{}”。 如果是,请看这里:

在这里:

还可以查看官方文档,因为可能有一个不带名称空间前缀的解析选项


德国劳埃德船级社。

建议很好,但我什么都没做。我尝试了一些代码示例的不同变体,但没有成功。我添加了
名称空间=”{http://www.w3.org/2005/Atom}“
listings=tree.findall('{%s}author/'%namespace)
并删除了
listings=tree.xpath(“//author”)
,但结果似乎相同:(名称空间确实是个问题,但您的解决方案并不是我想要的。不过谢谢您的帮助!
>>> for element in tree:
...     element
[...]
<Element {http://www.w3.org/2005/Atom}author at 7f14e75d1788>
[...]