Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ElementTree可以';似乎没有对findall()结果运行findall()_Python_Xpath_Elementtree - Fatal编程技术网

Python ElementTree可以';似乎没有对findall()结果运行findall()

Python ElementTree可以';似乎没有对findall()结果运行findall(),python,xpath,elementtree,Python,Xpath,Elementtree,我有如下形状的XML: <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:docs="http://schemas.google.com/docs/2007" xmlns:batch="http://schemas.google.com/gdat

我有如下形状的XML:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:docs="http://schemas.google.com/docs/2007" xmlns:batch="http://schemas.google.com/gdata/batch"
    <entry gd:etag="&quot;HxYZGQVeHyt7ImBr&quot;">
        <title>Some document title I wish to find</title>
这是有效的,返回:

WARNING:root:--------------------------------------------------------------------------------
WARNING:root:entry item found!
<ns0:entry ns1:etag="&quot;HxdWRh4MGit7ImBr&quot;" xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:ns1="http://schemas.google.com/g/2005">
    <ns0:title>
        Some document title
    </ns0:title>
</ns0:entry>
编辑:我最初在调试中使用了“document\u node[0].findall”。除去这个,上面的代码就可以工作了。这就是错误的原因-感谢下面的绅士发现了这一点

这会引发无标题节点的错误

这些结果似乎很奇怪,因为: -我可以在文档中清楚地看到带有该名称空间的元素 -我甚至可以使用该名称空间直接为title运行findall(),并查看结果

我想知道findall()返回的对象与输入的对象属于不同的类的可能性,但是在任何一个对象上运行“type”只会返回“instance”作为类型。那里有高质量的编程元素树


尽管LXML有更好的文档、更好的xpath支持和更好的代码,但出于技术原因,我不能使用LXML,因此我不得不使用ElementTree。

问题是,代码中的
文档节点[0]
已经引用了
标题
元素,查看它的子类不会返回任何结果。

FWIW,
实例
类型仅仅意味着它们使用的是旧样式的类(直到2001年12月的Python 2.2才引入新样式的类)。这是因为ElementTree支持1.5.2版的Python版本,所以它们不能使用新样式的类。@kindall-谢谢re:旧样式的类信息。我发誓我只是在使用整个文档后才包含[0]的,没有用。我已经移除了它,现在它确实移除了。谢谢你成为我的第二双眼睛!
WARNING:root:--------------------------------------------------------------------------------
WARNING:root:entry item found!
<ns0:entry ns1:etag="&quot;HxdWRh4MGit7ImBr&quot;" xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:ns1="http://schemas.google.com/g/2005">
    <ns0:title>
        Some document title
    </ns0:title>
</ns0:entry>
title = './/{http://www.w3.org/2005/Atom}title'
title_nodes = document_node.findall(title)
for title_node in title_nodes:
    logging.warn('yaaay')
    logging.warn(title_node.text)
if not title_nodes:
    raise ValueError('Could not find any title elements in this entry')