2

I looked at a few other answers but couldn't find a solution which worked for me.

Here's my complete code, which you can run without any API key:

import requests

r = requests.get('http://api.worldbank.org/v2/country/GBR/indicator/NY.GDP.MKTP.KD.ZG')

If I print r.text, I get a string that starts with

'\ufeff<?xml version="1.0" encoding="utf-8"?>\r\n<wb:data page="1" pages="2" per_page="50" total="60" sourceid="2" lastupdated="2019-12-20" xmlns:wb="http://www.worldbank.org">\r\n  <wb:data>\r\n    <wb:indicator id="NY.GDP.MKTP.KD.ZG">GDP growth (annual %)</wb:indicator>\r\n    <wb:country id="GB">United Kingdom</wb:country>\r\n    <wb:countryiso3code>GBR</wb:countryiso3code>\r\n    <wb:date>2019</wb:date>\r\n`

and goes on for a while.

One way of getting what I'd like out of it (which, as far as I understand, is heavily discouraged) is to use regex:

import regex

import pandas as pd
import re

pd.DataFrame(
    re.findall(
        r"<wb:date>(\d{4})</wb:date>\r\n    <wb:value>((?:\d\.)?\d{14})", r.text
    ),
    columns=["date", "value"],
)

What is a "proper" way of parsing this xml output? My final objective is to have a DataFrame with date and value columns, such as

    date    value
0   2018    1.38567356958762
1   2017    1.89207703836381
2   2016    1.91815510596298
3   2015    2.35552430595799
...
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65

3 Answers3

7

How about the following:

Decode the response:

decoded_response = response.content.decode('utf-8')

Convert to json:

response_json = json.loads(json.dumps(xmltodict.parse(decoded)))

Read into DataFrame:

pd.read_json(response_json) 

Then you just need to play with the orient and such (docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html)

Omri
  • 483
  • 4
  • 12
3

Full code I ended up using (based on Omri's excellent answer):

import xmltodict
import json
import pandas as pd

r = requests.get("http://api.worldbank.org/v2/country/GBR/indicator/NY.GDP.MKTP.KD.ZG")
decoded_response = r.content.decode("utf-8")

response_json = json.loads(json.dumps(xmltodict.parse(decoded_response)))

pd.DataFrame(response_json["wb:data"]["wb:data"])[["wb:date", "wb:value"]].rename(
    columns=lambda x: x.replace("wb:", "")
)

which gives

    date    value
0   2019    None
1   2018    1.38567356958762
2   2017    1.89207703836381
3   2016    1.91815510596298
4   2015    2.35552430595799
...
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
1

You can use ElementTree API (as described here )

import requests
from xml.etree import ElementTree

response = requests.get('http://api.worldbank.org/v2/country/GBR/indicator/NY.GDP.MKTP.KD.ZG')

tree = ElementTree.fromstring(response.content)

print(tree)

But you will have to explore the structure to get what you want.