tinyurl.com/sunbingo-uk

Get Stock Quote from Goolge Web API via Python

Background

Google has officially announce that it won't sustain ongoing of Finance API, but there still a available API via URL API . Return data from web server is xml format . We need to parse the return text in Python and extract data .

Get the Stock Data

Google Web API format (  http://www.google.com/ig/api?stock=C ) , this will request market data for security code "C" ( Citigroup  :) )  xml return result:

<xml_api_reply version="1">
<finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<symbol data="C"/>
<pretty_symbol data="C"/>
<symbol_lookup_url data="/finance?client=ig&q=C"/>
<company data="Citigroup Inc."/>
<exchange data="NYSE"/>
<exchange_timezone data="ET"/>
<exchange_utc_offset data="+05:00"/>
<exchange_closing data="960"/>
<divisor data="2"/>
<currency data="USD"/>
<last data="39.55"/>
<high data="39.75"/>
<low data="39.18"/>
<volume data="20298455"/>
<avg_volume data="37607"/>
<market_cap data="115981.19"/>
<open data="39.41"/>
<y_close data="39.38"/>
<change data="+0.17"/>
<perc_change data="0.43"/>
<delay data="0"/>
<trade_timestamp data="7 hours ago"/>
<trade_date_utc data="20121226"/>
<trade_time_utc data="210010"/>
<current_date_utc data="20121227"/>
<current_time_utc data="042731"/>
<symbol_url data="/finance?client=ig&q=C"/>
<chart_url data="/finance/chart?q=NYSE:C&tlf=12"/>
<disclaimer_url data="/help/stock_disclaimer.html"/>
<ecn_url data=""/>
<isld_last data="39.57"/>
<isld_trade_date_utc data="20121227"/>
<isld_trade_time_utc data="005615"/>
<brut_last data=""/>
<brut_trade_date_utc data=""/>
<brut_trade_time_utc data=""/>
<daylight_savings data="false"/>
</finance>
</xml_api_reply>

Now , we still use urlib library to fetch callback text from google web server . By using xml.dom.minidom ,we can get field "symbol" and it's attribute "data" value . Following code shows how to fetch symbol of tick.

>>> f = urllib.urlopen("http://www.google.com/ig/api?stock=C")
>>> s = f.read()
>>> from xml.dom.minidom import parseString
>>> dom  = parseString(s)
>>>dom.getElementsByTagName("symbol")[0].attributes['data'].value
u'C'

Construct in Python Class

Most of return XML data is quite self explained ,but I still double check with data feed from 3rd party source . As we may inspect , all xml element are formed in a consistent way-- “<NAME data="VALUE”/>

Thus, in constructing a Python Class, we need to iterate all elements of the xml tree and assign to the instance , A more dynamic style class.

from xml.dom.minidom import parseString


class Tick():
    def __init__(self, xml_string):
        from xml.dom.minidom import parseString
        dom = parseString(xml_string)
        self.symbol = self.get_att(dom,'symbol')
        self.company = self.get_att(dom,'company')
        self.exchange = self.get_att(dom,'exchange')
        self.currency = self.get_att(dom,'currency')
        self.last = self.get_att(dom,'last')
        self.high = self.get_att(dom,'high')
        self.low = self.get_att(dom,'low')
        self.volume = self.get_att(dom,'volume')
        self.change = self.get_att(dom,'change')
        self.y_close = self.get_att(dom,'y_close') 
        self.trade_date_utc= self.get_att(dom,'trade_date_utc') 
        self.isld_last = self.get_att(dom, 'isld_last') 

    def get_att(self, dom, name):
        return dom.getElementsByTagName(name)[0].attributes['data'].value

I've add a "get_att" method inside the class to shorten the text . Now the Tick object works as below

>>> from test import Tick
>>> t = Tick(s)
>>> t.low
u'39.18'
>>> t.high
u'39.75'

 Further More

Obviously , all attributes inside the Class Tick are string , but some fields are suppose to be a float ,like a price ,/ lowest price . This is a really one line code and I assume you are able to do that by your self . and again , this API is not officially guarantee by Google ,there may has change the field location or definition without notice , be caution when you try to apply this API in your Production System .

 

 

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>