parse api对python中XML的响应

发布于 2025-01-21 14:36:59 字数 1108 浏览 1 评论 0 原文

我正在使用下面的代码达到API链接:

import requests
url="https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items/web.config?versionType=Branch&versionOptions=None"
data=requests.get(url=url, headers=headers)

示例响应:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\MyApp.exe"
                  stdoutLogEnabled="false"
                  stdoutLogFile=".\logs\stdout"
                  hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>
<PropertyGroup>
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

我想对XML的API响应解析并提取环境名称,即从此XML开发。请协助

I am hitting an api link with the code below:

import requests
url="https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items/web.config?versionType=Branch&versionOptions=None"
data=requests.get(url=url, headers=headers)

The sample response:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\MyApp.exe"
                  stdoutLogEnabled="false"
                  stdoutLogFile=".\logs\stdout"
                  hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>
<PropertyGroup>
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

I want to parse the api response to xml and extract the EnvironmentName, that is Development from this xml. Please assist

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2025-01-28 14:36:59

您可以通过在字符串中简单搜索来实现这一目标,
像这样

import re
result = re.search('<EnvironmentName>;(.*)</EnvironmentName>', response)

或通过搜索可以将响应变成对象然后通过其键迭代的LIB

you could achive this by simple searching in the string,
like this

import re
result = re.search('<EnvironmentName>;(.*)</EnvironmentName>', response)

or by searching up a lib that can turn the response into an object and then iterating through its keys

乖乖兔^ω^ 2025-01-28 14:36:59

我列出了两个可以用来解决您的问题的软件包。
我使用了免费的在线示例XML API 来测试您的问题并编写相关代码。

  1. xmltodict

需要下载此库,可以通过以下方式通过PIP完成 pip install xmltodict

resp = requests.get('http://restapi.adequateshop.com/api/Traveler?page=6')
dict_of_xml = xmltodict.parse(resp.content)

您将获得这样的输出。

订购([[('TravelerInformation',
[orderddict([[('id','776'),
('姓名',
'Yaamini Yaamini'),),
('电子邮件',
'

  1. https://docs.python.org/2/library/xml.etree.elementree.elementtree.html#

此软件包不需要任何其他安装,但是使用情况更为复杂。

您可以这样导入包裹。

from xml.etree import ElementTree

要将响应转换为可以使用的树。

resp = requests.get('http://restapi.adequateshop.com/api/Traveler?page=6')
tree = ElementTree.fromstring(resp.content)

要访问数据,您将必须使用响应的索引,这有点像这样。
树[4] 0 .text
您将获得这样的输出。

'yaamini yaamini'

I am listing two packages that can be used to solve your problem.
I have used a free online sample XML API to test your problem and write relevant code.

  1. xmltodict.

This library needs to be downloaded which can be done via pip in the following way pip install xmltodict

resp = requests.get('http://restapi.adequateshop.com/api/Traveler?page=6')
dict_of_xml = xmltodict.parse(resp.content)

You will get an output like this.

OrderedDict([('Travelerinformation',
[OrderedDict([('id', '776'),
('name',
'Yaamini Yaamini'),
('email',
'[email protected]'),
('adderes', 'USA'),
('createdat',
'2020-10-08T09:56:30.7263582')])

  1. xmltree

This package does not need any additional installation but usage is somewhat more complex.

You can import the package like this.

from xml.etree import ElementTree

To convert the response into the tree you can use.

resp = requests.get('http://restapi.adequateshop.com/api/Traveler?page=6')
tree = ElementTree.fromstring(resp.content)

To access the data then you will have to use indices of the response, which is somewhat like this.
tree[4]0.text
You will get an output like this.

'Yaamini Yaamini'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文