【问题标题】:xml parse from API response从 API 响应解析 xml
【发布时间】:2020-10-27 18:58:57
【问题描述】:

我正在试验一个输出 XML 的 API。

谁能告诉我解析 API 响应的代码是否有问题,或者是 API 本身的问题?

我可以使用put 命令:

import requests
import xml
from xml.etree import ElementTree

response = requests.post("https://openadrtester.herokuapp.com/api/v1/cpp/senorio/three")
string_xml = response.content
tree = xml.etree.ElementTree.fromstring(string_xml)

xml.etree.ElementTree.dump(tree)

但它会出错:

Traceback (most recent call last):
  File "C:\Users\bbartling\OneDrive - Slipstream\Desktop\myAPI\tut\test.py", line 7, in <module>
    tree = xml.etree.ElementTree.fromstring(string_xml)
  File "C:\Users\bbartling\AppData\Local\Programs\Python\Python37\lib\xml\etree\ElementTree.py", line 1315, in XML
    parser.feed(text)
  File "<string>", line None
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0

XML 也可以通过浏览器在获取请求时查看:

https://openadrtester.herokuapp.com/

【问题讨论】:

  • 如果你在代码中print() string_xml 包含什么?
  • b'{"notification": "True", "startTime": "2:00PM", "duration": "6 Hours", "randomization": "None", "rampUp": “无”,“恢复”:“无”,“numberOfSignals”:“2”,“signalNameSimple”:[{“signalType”:“级别”,“单位”:“无”,“numberOfIntervals”:“无”, “intervalDuration”:“None”,“typicalIntervalValues”:“1,2,1”,“signalTarget”:“None”}],“signalNameElectricityPrice”:[{“signalType”:“price”,“units”:“USD每千瓦时”,“numberOfIntervals”:“3”,“intervalDuration”:“1 小时,4 小时,1 小时”,“典型的IntervalValues”:“$0.50,$0.75,$0​​.50”,“signalTarget”:“无”}]}\ n'
  • 这是 json,不是 XML。

标签: python xml api python-requests


【解决方案1】:

数据不是XML,而是Json(用.json()方法解析,然后像普通python结构一样访问):

import json
import requests

response = requests.post("https://openadrtester.herokuapp.com/api/v1/cpp/senorio/three")
data = response.json()

print(json.dumps(data, indent=4))

打印:

{
    "notification": "True",
    "startTime": "2:00PM",
    "duration": "6 Hours",
    "randomization": "None",
    "rampUp": "None",
    "recovery": "None",
    "numberOfSignals": "2",
    "signalNameSimple": [
        {
            "signalType": "level",
            "units": "None",
            "numberOfIntervals": "None",
            "intervalDuration": "None",
            "typicalIntervalValues": "1,2,1",
            "signalTarget": "None"
        }
    ],
    "signalNameElectricityPrice": [
        {
            "signalType": "price",
            "units": "USD per kWh",
            "numberOfIntervals": "3",
            "intervalDuration": "1 hour,4 hour,1 hour",
            "typicalIntervalValues": "$0.50,$0.75,$0.50",
            "signalTarget": "None"
        }
    ]
}

编辑:修改请求标头以获取 XML:

import requests

response = requests.post("https://openadrtester.herokuapp.com/api/v1/cpp/senorio/three", headers={'Accept': 'application/xml'})
print(response.text)

打印:

<?xml version="1.0" ?><response><notification>True</notification><startTime>2:00PM</startTime><duration>6 Hours</duration><randomization>None</randomization><rampUp>None</rampUp><recovery>None</recovery><numberOfSignals>2</numberOfSignals><signalNameSimple><signalType>level</signalType><units>None</units><numberOfIntervals>None</numberOfIntervals><intervalDuration>None</intervalDuration><typicalIntervalValues>1,2,1</typicalIntervalValues><signalTarget>None</signalTarget></signalNameSimple><signalNameElectricityPrice><signalType>price</signalType><units>USD per kWh</units><numberOfIntervals>3</numberOfIntervals><intervalDuration>1 hour,4 hour,1 hour</intervalDuration><typicalIntervalValues>$0.50,$0.75,$0.50</typicalIntervalValues><signalTarget>None</signalTarget></signalNameElectricityPrice></response>

【讨论】:

  • 当人们使用curl -i -H "Accept: application/xml" 时,-H 是一个标头参数吗?
  • 如果是这样修改我的requests 脚本是否容易?
  • 你能帮我解决这个问题吗? :) stackoverflow.com/questions/64573932/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多