Using Python to get the BSE Stock Price

January 26th, 2010 by Kevin | Posted under programs.

This post explains how to get the BSE stock price of a company using the company code. I found three methods to be able to do this.

1) Use the Google API to get the data from Google Search.

2) Use the Stock Market API to get the data.

3) Use the BSE Website API to get the data.

I would like to explore methods 1 and 2 further but for now I have something for method 3. This method is not new and I use the knowledge provided by Mayank Sharma using Ruby. The idea makes use of the fact that when we search by using the company code on the BSE website, it acts as a referrer to the actual server to provide the necessary information which is returned back in a comma separated form.

Following are the steps involved:

1) http://www.bseindia.com/stockreach/stockreach.htm is the BSE website to search by company code. When we search by company code, the method used is

http://www.bseindia.com/stockreach/stockreach.htm?scripcd=<here the company code as parameter>

eg: If we are searching for HDFCBANK(Code: 500180), then the URL is http://www.bseindia.com/stockreach/stockreach.htm?scripcd=500180

2) Once this is done, a request is sent to http://www.bseindia.com/DotNetStockReachs/DetailedStockReach.aspx to get the data. There are two parameters passed, GUID which is a random number and the company code. This request looks like

http://www.bseindia.com/DotNetStockReachs/DetailedStockReach.aspx?GUID=0b35e756987938&scripcd=<here the company code as parameter>

eg: If we are searching for HDFCBANK(Code: 500180),

then the URL is

http://www.bseindia.com/DotNetStockReachs/DetailedStockReach.aspx?GUID=0b35e756987938&scripcd=500180

3) We get the data as a list of comma separated values which we can sort out by what they represent on the site.

Here is the Python script to perform this retrieval of BSE stock information. The script is written using Python 3.1. It makes use of the httplib2 HTTP library for Python.

import httplib2

def get_bse(code):

     """Retrieve the BSE information based on the BSE Company Code entered"""

     url_string = "http://www.bseindia.com/DotNetStockReachs/DetailedStockReach.aspx?GUID=0b35e756987938&scripcd=" + str(code)
     referer = "http://www.bseindia.com/stockreach/stockreach.htm?scripcd=" + str(code)

     h = httplib2.Http(".cache")
     resp, content = h.request(url_string, "GET", headers={'Referer': referer})

     string = str(content, encoding='utf8')
     str_list = string.split(',')
     print(str_list)

if __name__ == '__main__':
     get_bse(500180)   #Get the stock iformation for HDFCBANK with code 500180

The ‘referer’ is required by the actual serving URL to know from where the request has originated. In this case, from ‘url_string’. This is being sent in the header of the http GET request. If you do not sent the ‘referer’, your request will be rejected.

 

Related Posts:

Tags:

Comments

2 Responses to “Using Python to get the BSE Stock Price”
  1. venu says:

    was searching for something like this.
    However, being a non techincal (computer/ coding) lay person

    I am not aware, nor could find a way to use you excellent method
    posted about “Using Python to get the BSE Stock Price” to actual use.

    Please can you guide of a way to use a normal HTML page or some utilty for
    fething the BSE prices LIVE. If you can email it-> khushistocks {AT} gmail {DOT} com

    Thanks in advance.

    Regards
    Venu

    [Reply]

    Kevin Reply:

    Hi Venu,

    You can find the BSE prices live at the BSE website itself. Or you can also use commercial sites like icicidirect, etc.

    [Reply]

Do you have any comments on Using Python to get the BSE Stock Price ?

Spam protection by WP Captcha-Free