Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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 etree元素上的xpath产生意外结果_Python_Xpath_Lxml - Fatal编程技术网

Python etree元素上的xpath产生意外结果

Python etree元素上的xpath产生意外结果,python,xpath,lxml,Python,Xpath,Lxml,我正在运行xpath来过滤带有“item”标记的XML提要。从结果列表中,我获取第一个结果并使用xpath过滤“title”标记。然而,当我过滤“title”时,我从没有“item”标记的xml中获取一个标题。由于我正在对“item”结果集执行xpath,因此这种行为是意外的。谁能告诉我这里发生了什么事 请参阅下面使用xpath的代码 from urllib.request import urlopen from lxml import etree url = 'https://www.sec.

我正在运行xpath来过滤带有“item”标记的XML提要。从结果列表中,我获取第一个结果并使用xpath过滤“title”标记。然而,当我过滤“title”时,我从没有“item”标记的xml中获取一个标题。由于我正在对“item”结果集执行xpath,因此这种行为是意外的。谁能告诉我这里发生了什么事

请参阅下面使用xpath的代码

from urllib.request import urlopen
from lxml import etree
url = 'https://www.sec.gov/Archives/edgar/monthly/xbrlrss-2018-02.xml'
data = urlopen(url)
xml = data.read()
parser = etree.XMLParser(remove_blank_text=True, huge_tree=True)
root = etree.XML(xml, parser=parser)
items = root.xpath("//item")
first_item = items[0]
title = first_item.xpath("//title")[0].text
print(title)
#'All XBRL Data Submitted to the SEC for 2018-02'
我希望第一项是:

<item>
<title>DST SYSTEMS INC (0000714603) (Filer)</title>
<link>http://www.sec.gov/Archives/edgar/data/714603/000071460318000013/0000714603-18-000013-index.htm</link>
<guid>http://www.sec.gov/Archives/edgar/data/714603/000071460318000013/0000714603-18-000013-xbrl.zip</guid>
<enclosure url="http://www.sec.gov/Archives/edgar/data/714603/000071460318000013/0000714603-18-000013-xbrl.zip" length="470442" type="application/zip" />
<description>10-K</description>
<pubDate>Wed, 28 Feb 2018 17:29:39 EST</pubDate>
<edgar:xbrlFiling xmlns:edgar="http://www.sec.gov/Archives/edgar"></item>

DST系统公司(0000714603)(文件管理器)
http://www.sec.gov/Archives/edgar/data/714603/000071460318000013/0000714603-18-000013-index.htm
http://www.sec.gov/Archives/edgar/data/714603/000071460318000013/0000714603-18-000013-xbrl.zip
10-K
2018年2月28日星期三美国东部时间17:29:39
相反,当我这样做时: title=first_item.xpath(“//title”)。文本,我得到的标题是“2018-02年提交给SEC的所有XBRL数据”

标题来自:

<channel>
<title>All XBRL Data Submitted to the SEC for 2018-02</title>
<link>http://www.sec.gov/spotlight/xbrl/filings-and-feeds.shtml</link>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" href="http://www.sec.gov/Archives/edgar/monthly/xbrlrss-2018-02.xml" rel="self" type="application/rss+xml" />
<description>This is a list all of the filings containing XBRL for 2018-02</description>
<language>en-us</language>
<pubDate>Wed, 28 Feb 2018 00:00:00 EST</pubDate>
<lastBuildDate>Wed, 28 Feb 2018 00:00:00 EST</lastBuildDate>

2018-02年度提交给SEC的所有XBRL数据
http://www.sec.gov/spotlight/xbrl/filings-and-feeds.shtml
这是2018-02年度包含XBRL的所有文件清单
美国英语
2018年2月28日星期三美国东部时间00:00:00
2018年2月28日星期三美国东部时间00:00:00
但是我在items上运行了xpath,它没有xpath(“items”)。我不确定为什么我没有得到“DST SYSTEMS INC(000071463)(文件管理器)”的预期结果。

而不是:

title = first_item.xpath("//title")[0].text
使用:

“标题”之前的区别是“/”

原因是“//title”选择所有标题元素,无论它们在文档中的什么位置。只需使用“title”即可选择名为“title”的节点

title = first_item.xpath("title")[0].text