ExchangeList() - Eoddata.com web service blog series
Sat 25 March 2017 by Adrian TorrieThis post is part 3 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
ExchangeList
call: http://ws.eoddata.com/data.asmx?op=ExchangeList
Version Control¶
In [1]:
%run ../../code/version_check.py
Change Log¶
Date Created: 2017-03-25
Date of Change Change Notes
-------------- ----------------------------------------------------------------
2017-03-25 Initial draft
2017-04-02 - Changed any references for `get_exchange_list()` to `exchange_list()`
- Client class function returns data in fixed order now
Setup¶
In [10]:
%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()
ExchangeList()¶
Web service call¶
In [3]:
session = r.Session()
call = 'ExchangeList'
kwargs = {'Token': token,}
pattern = ".//{%s}EXCHANGE"
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]:
exchanges = sorted(element.get('Code') for element in elements)
exchanges
Out[5]:
Save to file¶
In [6]:
with open('../../data/exchanges.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 ExchangeList(session, token):
call = 'ExchangeList'
kwargs = {'Token': token,}
pattern = ".//{%s}EXCHANGE"
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') for element in elements)
Usage¶
In [10]:
session = r.session()
exchanges = ExchangeList(session, token)
exchanges
session.close()
Client function¶
In [11]:
# pandas dataframe is returned
df = eoddata.exchange_list()
df.head()
Out[11]: