Python 命令的解析器输出

Python parser output from command

我使用 curl 命令检索此输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><restQueuedBuild planKey="project-build001" buildNumber="123" buildResultKey="project-build001-123"><triggerReason>Manual build</triggerReason><link href="https://mybamboo.server.com/rest/api/latest/result/project-build001-123" rel="self"/></restQueuedBuild>

如何获得 in buildNumber 卷 123 in python?

试试这个:

import xml.etree.ElementTree as ET
root = ET.fromstring('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><restQueuedBuild planKey="project-build001" buildNumber="123" buildResultKey="project-build001-123"><triggerReason>Manual build</triggerReason><link href="https://mybamboo.server.com/rest/api/latest/result/project-build001-123" rel="self"/></restQueuedBuild>')


print(root.get('buildNumber'))

您需要使用 BeautifulSoup 包。你可以像那样通过 pip 安装

pip install BeautifulSoup

这样做你会得到想要的结果

from bs4 import BeautifulSoup

soup = BeautifulSoup('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><restQueuedBuild planKey="project-build001" buildNumber="123" buildResultKey="project-build001-123"><triggerReason>Manual build</triggerReason><link href="https://mybamboo.server.com/rest/api/latest/result/project-build001-123" rel="self"/></restQueuedBuild>', 'lxml')

subResult = soup.find('restqueuedbuild')
result = subResult['buildnumber']
print result

字体:https://linuxhint.com/parse_xml_python_beautifulsoup/