findtext和xpath没有从已解析的xml文档中返回任何内容

2024-04-27 04:13:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在django中使用lxml.etree.parse来解析外部rss提要中的一些内容,并使用findall来解决名称空间问题。在

我可以反复查看结果,但我无法显示结果中的任何文本。在

下面是我试图从中获取的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 is a book full of words
        </content>
    </entry>
</feed>

以下是我对django的看法:

^{pr2}$

我也尝试过用xpath代替findtext,但是得到了相同的结果。在

    "link":listing.xpath("link/text()"),
    "title":listing.xpath("entry/link/text()"),
    "content":listing.xpath("content/text()"),

感谢任何帮助。在


Tags: nameorgidhttptitlewwwfeedlink
1条回答
网友
1楼 · 发布于 2024-04-27 04:13:00

您没有考虑XML名称空间。在

tree = lxml.etree.parse("http://www.somedomain.org/people/atom/author_name")
xmlns = {"atom": "http://www.w3.org/2005/Atom"}
listings = tree.xpath("//atom:entry", namespaces=xmlns)

listings_info = []

for listing in listings:
    listings_info.append({
        "link": listing.xpath("./atom:link/@href", namespaces=xmlns),
        "title": listing.xpath("./atom:title", namespaces=xmlns),
        "content": listing.xpath("./atom:content", namespaces=xmlns),
    })

必须定义前缀(即使XML中没有前缀)并在XPath表达式中使用它。这意味着您必须通知.xpath()您将为哪个名称空间使用哪个前缀,因此需要使用第二个参数。在

相关问题 更多 >