parse api对python中XML的响应
我正在使用下面的代码达到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开发。请协助
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在字符串中简单搜索来实现这一目标,
像这样
或通过搜索可以将响应变成对象然后通过其键迭代的LIB
you could achive this by simple searching in the string,
like this
or by searching up a lib that can turn the response into an object and then iterating through its keys
我列出了两个可以用来解决您的问题的软件包。
我使用了免费的在线
示例XML API
来测试您的问题并编写相关代码。需要下载此库,可以通过以下方式通过PIP完成
pip install xmltodict
您将获得这样的输出。
此软件包不需要任何其他安装,但是使用情况更为复杂。
您可以这样导入包裹。
要将响应转换为可以使用的树。
要访问数据,您将必须使用响应的索引,这有点像这样。
树[4] 0 .text
您将获得这样的输出。
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.This library needs to be downloaded which can be done via pip in the following way
pip install xmltodict
You will get an output like this.
This package does not need any additional installation but usage is somewhat more complex.
You can import the package like this.
To convert the response into the tree you can use.
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.