我编写了一个简单的 Python 脚本来打印与父元素属性关联的所有包名称的 XML 数据:Security Advisory。import xml.etree.ElementTree as ETtree = ET.parse('errata.xml')root = tree.getroot()for security in root.findall("*[@type='Security Advisory']"): packages = security.find('packages') print(packages.text)但是,该脚本仅打印出第一个包名称,但有多个包名称。我将如何获取属于父属性的所有包名称:Security Advisory?
1 回答
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
下面(似乎有效)
import xml.etree.ElementTree as ET
import requests
r = requests.get('https://cefs.b-cdn.net/6010e333a44911e24b5112e23acbb346ae15f7b7/errata.latest.xml')
if r.status_code == 200:
root = ET.fromstring(r.content)
sec_elements = [e for e in root.findall("*[@type='Security Advisory']") if
e.find('os_release') is not None and int(e.find('os_release').text) > 6]
for ele in sec_elements:
packages = ele.findall('./packages')
for p in packages:
print(p.text)
添加回答
举报
0/150
提交
取消