使用 Python 保存来自 GET 调用的 XML 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29810572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:02:41  来源:igfitidea点击:

Save XML response from GET call using Python

pythonxmlsavepython-requestsparsexml

提问by Julio Montes

I'm trying to create a realtime report using an API that allows me to grab the data I need and returns it in XML format. What I want to know is, after receiving the response, how can I save it to an .xml file locally? Or cache it, that way I can parse it before parsing the response.

我正在尝试使用 API 创建实时报告,该 API 允许我获取所需的数据并以 XML 格式返回。我想知道的是,在收到响应后,如何将其保存到本地的 .xml 文件中?或者缓存它,这样我就可以在解析响应之前解析它。

import requests
r = requests.get('url',  auth=('user', 'pass'))

I'm using requests since it's the easiest way to make a GET call in my opinion. Also, this is my first question and I'm barely starting to learn Python, I'd appreciate it if you guys had a little patience. Thanks.

我正在使用请求,因为在我看来这是进行 GET 调用的最简单方法。另外,这是我的第一个问题,我才刚刚开始学习 Python,如果你们有一点耐心,我将不胜感激。谢谢。

I was looking at a similar question but for JSON, not sure if it would work the same, https://stackoverflow.com/a/17519020/4821590

我在看一个类似的问题,但对于 JSON,不确定它是否会起作用,https://stackoverflow.com/a/17519020/4821590

import requests
import json
solditems = requests.get('https://github.com/timeline.json') # (your url)
data = solditems.json()
with open('data.json', 'w') as f:
    json.dump(data, f)

采纳答案by enigma

If you want to be able to parse the returned XML before doing stuff with it, the xml treeis your friend.

如果您希望能够在处理返回的 XML 之前对其进行解析,那么xml 树就是您的朋友。

import requests
import xml.etree.ElementTree as ET

r = requests.get('url',  auth=('user', 'pass'))
tree = ET.parse(r.text)
root = tree.getroot()

Otherwise, as jordanm has commented, you could just save it to a file and be done with it.

否则,正如 jordanm 所评论的,您可以将它保存到一个文件中并完成它。

with open('data.xml', 'w') as f:
    f.write(r.text)

回答by j5awry

Few notes related to Python3 (at least 3.6 versions):

Python3相关的几个注意事项(至少3.6版本):

1) when using xml.etree.ElementTree with requests, you use fromstring not parse. r.text returns a string, and xml.etree.ElementTree.parse is for files

1) 在请求中使用 xml.etree.ElementTree 时,使用 fromstring 不解析。r.text 返回一个字符串,xml.etree.ElementTree.parse 用于文件

import requests
import xml.etree.ElementTree as ET

r = requests.get("https://xml.returning.uri")
root = ET.fromstring(r.text)

2) This creates an element object as the root (no more tree). So to write it back out, you'll need to make it a tree:

2)这会创建一个元素对象作为根(不再是树)。所以要把它写回来,你需要把它变成一棵树:

tree = ET.ElementTree(root)
tree.write("file.xml")

From the docs

从文档

xml.etree.ElementTree.parse(source, parser=None) Parses an XML section into an element tree. source is a filename or file object containing XML data.

xml.etree.ElementTree.fromstring(text) Parses an XML section from a string constant. Same as XML(). text is a string containing XML data. Returns an Element instance

xml.etree.ElementTree.parse(source, parser=None) 将 XML 部分解析为元素树。source 是包含 XML 数据的文件名或文件对象。

xml.etree.ElementTree.fromstring(text) 从字符串常量解析 XML 部分。与 XML() 相同。text 是一个包含 XML 数据的字符串。返回一个 Element 实例