【问题标题】:Python parser output from command命令的 Python 解析器输出
【发布时间】:2021-05-13 14:52:22
【问题描述】:

我使用 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>

如何在python中获取 buildNumber volue 123

【问题讨论】:

  • 您搜索了什么,找到了什么?你尝试过什么,它是如何失败的?

标签: python parsing output extract


【解决方案1】:

试试这个:

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'))

【讨论】:

    【解决方案2】:

    你需要使用 BeautifulSoup 包。你可以像这样通过 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多