For personal work.
You can get the response in XML with the Ministry of Internal Affairs and Communications API. Here is a summary of how to parse this XML. I plan to use it for work when the beginning of the week is free. The sample code of the Ministry of Internal Affairs and Communications API describes how to get social and demographic statistics, and when you hit it, the following XML will be returned.
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<GET_STATS_DATA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.e-stat.go.jp/rest/2.0/schema/GetStatsData.xsd">
<RESULT>
<STATUS>0</STATUS>
<ERROR_MSG>It ended normally.</ERROR_MSG>
<DATE>2016-09-04T21:28:09.920+09:00</DATE>
</RESULT>
<PARAMETER>
<LANG>J</LANG>
<STATS_DATA_ID>C0020050213000</STATS_DATA_ID>
<NARROWING_COND>
<CODE_CAT01_SELECT>#A03503</CODE_CAT01_SELECT>
</NARROWING_COND>
<DATA_FORMAT>X</DATA_FORMAT>
Omitted below
It's like getting this using the urllib module and accessing the element using the xml module. If it is an old API, JSON output may not be available.
This is written in various places, and if you look at the official document, you can find it in various places. The endpoint URL of the Ministry of Internal Affairs and Communications API is used, but please log in to obtain the application ID.
import urllib
url = 'http://api.e-stat.go.jp/rest/2.0/app/XXXXXXXXXXXX'
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as response:
XmlData = response.read()
This will store the XML text data in XmlData.
Import the XML module and start parsing. This is also detailed in the official documentation.
import xml.etree.ElementTree as ET
root = ET.fromstring(XmlData)
only this.
#Top level tags and contents
print(root.tag,root.attrib)
#Child hierarchy tags and contents
for child in root:
print(child.tag, child.attrib)
If you want to include grandchild elements, isn't it better to create a list or dictionary and write it?
It's the same as accessing the list.
print(root[0][1].text)
I think I can do what I want to do for the time being.
Recommended Posts