[英]How to get all strings from all nested tags of a xml tag with python's lxml.etree library?
[英]How to extract text from lxml.etree tags based on value of sibling tags
我的目标是从xml文档(链接)中提取URL并将其放在列表中: https : //www.valuespreadsheet.com/iedgar/results.php? stock=NFLX&output=xml
我从lxml
导入etree
并创建了一个列表etree
,该列表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。
我该如何实现?
您需要一个XPath表达式和xpath()
方法 :
[url.text for url in et.xpath('//filing[formType = "10-K"]/instanceUrl')]
在这里,我们正在过滤包含带有10-K
文本的formType
子节点的filing
节点,然后获取instanceUrl
子节点。
请注意, _
变量名用于一次性变量 -必须定义但未实际使用的变量(例如,在解压缩过程中)。 就您而言,您实际上已经使用过它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.