CountryList() - Eoddata.com web service blog series
Sun 02 April 2017 by Adrian TorrieThis post is part 4 of the "Using Python with Eoddata.com web service" series:
- Master post - Eoddata.com web service blog series
- Login() - Eoddata.com web service blog series
- ExchangeList() - Eoddata.com web service blog series
- CountryList() - Eoddata.com web service blog series
- SymbolList() - Eoddata.com web service blog series
- FundamentalList() - Eoddata.com web service blog series
Summary¶
Part of the blog series related to making web service calls to Eoddata.com. Overview of the web service can be found here.
- View the master post of this series to build a secure credentials file. It is used in all posts related to this series.
- Download this blog post as a jupyter notebook
- Download the class definition file for an easy to use client, which is demonstrated below
- This post covers the
CountryList
call: http://ws.eoddata.com/data.asmx?op=CountryList
Version Control¶
In [1]:
%run ../../code/version_check.py
Change Log¶
Date Created: 2017-04-02
Date of Change Change Notes
-------------- ----------------------------------------------------------------
2017-04-02 Initial draft
Setup¶
In [2]:
%run ../../code/eoddata.py
import pandas as pd
import requests as r
ws = 'http://ws.eoddata.com/data.asmx'
ns='http://ws.eoddata.com/Data'
with (Client()) as eoddata:
token = eoddata.get_token()
CountryList()¶
Web service call¶
In [3]:
session = r.Session()
call = 'CountryList'
kwargs = {'Token': token,}
pattern = ".//{%s}CountryBase"
url = '/'.join((ws, call))
response = session.get(url, params=kwargs, stream=True)
if response.status_code == 200:
root = etree.parse(response.raw).getroot()
session.close()
Gather elements¶
In [4]:
elements = root.findall(pattern %(ns))
Get data¶
In [5]:
countries = sorted((element.get('Code'), element.get('Name')) for element in elements)
countries
Out[5]:
Save to file¶
In [6]:
with open('../../data/countries.csv', 'w') as f:
for element in elements:
f.write('"%s"\n' % '","'.join(element.attrib.values()))
Data inspection¶
In [7]:
for item in root.items():
print (item)
In [8]:
for element in root.iter():
print(element.attrib)
Helper function¶
In [9]:
def CountryList(session, token):
call = 'CountryList'
kwargs = {'Token': token,}
pattern = ".//{%s}CountryBase"
url = '/'.join((ws, call))
response = session.get(url, params=kwargs, stream=True)
if response.status_code == 200:
root = etree.parse(response.raw).getroot()
return sorted((element.get('Code'), element.get('Name')) for element in elements)
Usage¶
In [10]:
session = r.session()
countries = CountryList(session, token)
session.close()
countries[:10]
Out[10]:
Client function¶
In [11]:
# pandas dataframe is returned
df = eoddata.country_list()
df.head()
Out[11]: