为什么此xpath表达式返回一个空列表? [英] Why does this xpath expression return an empty list?

查看:820
本文介绍了为什么此xpath表达式返回一个空列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析此 XML .这是YouTube供稿.我正在根据教程中的代码进行工作.我想获取嵌套在feed下的所有entry节点.

I'm trying to parse this XML. It's a YouTube feed. I'm working based on code in the tutorial. I want to get all the entry nodes that are nested under the feed.

from lxml import etree
root = etree.fromstring(text)
entries = root.xpath("/feed/entry")
print entries

由于某些原因,entries是一个空列表.为什么?

For some reason entries is an empty list. Why?

推荐答案

feed及其所有子代实际上都在http://www.w3.org/2005/Atom命名空间中.您需要告诉xpath:

feed and all its children are actually in the http://www.w3.org/2005/Atom namespace. You need to tell your xpath that:

entries = root.xpath("/atom:feed/atom:entry", 
                     namespaces={'atom': 'http://www.w3.org/2005/Atom'})

或者,如果您想更改默认的空命名空间:

or, if you want to change the default empty namespace:

entries = root.xpath("/feed/entry", 
                     namespaces={None: 'http://www.w3.org/2005/Atom'})

或者,如果您根本不想使用短句柄:

or, if you don't want to use shorthandles at all:

entries = root.xpath("/{http://www.w3.org/2005/Atom}feed/{http://www.w3.org/2005/Atom}entry")

据我所知,您正在使用的节点隐式地假定了本地名称空间",因此对同一名称空间中子级的操作不需要您再次设置它.因此,您应该能够执行以下操作:

To my knowledge the "local namespace" is implicitly assumed for the node you're working with so that operations on children in the same namespace do not require you to set it again. So you should be able to do something along the lines of:

feed = root.find("/atom:feed",
                     namespaces={'atom': 'http://www.w3.org/2005/Atom'})

title = feed.xpath("title")
entries = feed.xpath("entries")
# etc...

这篇关于为什么此xpath表达式返回一个空列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆