我使用Python中的请求库来处理公司的API。你知道吗
但是结果是XML格式的。你知道吗
如何在Python或使用请求中引用所需的数据?有没有办法把这些数据转换成JSON?你知道吗
举个例子:
XML格式:
<result xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Starbucks.Location.WebAPI.Models">
<paging>
<total>21933</total>
<offset>0</offset>
<limit>10</limit>
<returned>10</returned>
</paging>
<stores>
<store>
<id>1</id>
<name>Store1</name>
<brandName>BrandName</brandName>
<storeNumber>34601-20281</storeNumber>
</store>
<store>
<id>2</id>
<name>Store2</name>
<brandName>BrandName</brandName>
<storeNumber>20281</storeNumber>
</store>
我的Python代码看起来有点像这样:
for i in range(0, 1):
myParameters = {'limit': '50', 'offset': i*50}
response = requests.get(url, params=myParameters)
print response #Here is where I want to print the data in json format
在该示例中,响应返回xml格式。你知道吗
我想创建一个csv或文本文件或使用每个商店的特定属性的东西。例如,获取商店id并将其放置在csv文件的列中。你知道吗
但是我不知道如何在xml结果中引用id标记。我认为将数据转换为json会有所帮助,但当我使用Requests.json()或json库时,会出现错误:No JSON object could be decoded
但是,有许多XML解析器—由于您对获取JSON感兴趣,您可能希望尝试使用
xmltodict
和json
,这可以让您在一行中就到达那里。你知道吗相关问题 更多 >
编程相关推荐