如何从中提取文本lxml.etree文件基于同级标记值的标记

2024-06-28 14:35:27 发布

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

我的目标是从xml文档(链接)中提取URL并将其放入列表中: https://www.valuespreadsheet.com/iedgar/results.php?stock=NFLX&output=xml

我从lxml导入了etree,并创建了一个从所有<instanceUrl>标记中提取文本的列表理解。你知道吗

url = 'https://valuespreadsheet.com/iedgar/results.php?stock=NFLX&output=xml' 
et = etree.fromstring(urlopen(url).read())
return [_.find('instanceUrl').text for _ in et.find('filings')]

现在,我想限制列表,以便它只从<instanceUrl>标记中提取文本,其中<formType>=10K

我怎样才能做到这一点?你知道吗


Tags: https标记文本com列表outputstockxml
1条回答
网友
1楼 · 发布于 2024-06-28 14:35:27

你需要一个XPath expression and the ^{} method

[url.text for url in et.xpath('//filing[formType = "10-K"]/instanceUrl')]

这里我们用10-K文本过滤包含formType子节点的filing节点,然后得到instanceUrl子节点。你知道吗

请注意,_变量名用于丢弃变量-必须定义但未实际使用的变量(例如在解包期间)。在你的情况下,你实际上已经用过了。你知道吗

相关问题 更多 >