簡體   English   中英

Python 解析器 output 來自命令

[英]Python parser output from command

我使用 curl 命令檢索此 output:

<?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>

如何在 python 中獲取buildNumber volue 123

嘗試這個:

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 package。 您可以像這樣通過 pip 安裝

pip 安裝 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM