There are many ways to extract stocks information using python. A simple way to get the current stocks data can be achieved by using python Pandas. The data retrieved however are limited.
The method I use below are based on downloading the various data .csv file, a service provided by the Yahoo Finance. The method to construct the various url to download the .csv information are described in great details from the Yahoo Finance API.
The current script created can only retrieved the most current data statistics for the various stocks. First, it will construct the URL based on user stocks input and the parameters required. It then makes use of the PATTERN module to read the url and download the information to local drive. Next, it will call the pandas function to read the .csv file and convert it to data frame for further analysis.
Sample output of the script is as shown below.
data_ext = YFinanceDataExtr() ## Specify the stocks to be retrieved. Each url constuct max up to 50 stocks. data_ext.target_stocks = ['S58.SI','S68.SI'] #special character need to be converted ## Get the url str data_ext.form_url_str() print data_ext.cur_quotes_full_url ## >>> http://download.finance.yahoo.com/d/quotes.csv?s=S58.SI,S68.SI&f=nsl1opvkj&e=.csv ## Go to url and download the csv. ## Stored the data as pandas.Dataframe. data_ext.get_cur_quotes() print data_ext.cur_quotes_df ## >>> NAME SYMBOL LATEST_PRICE OPEN CLOSE VOL YEAR_HIGH YEAR_LOW ## >>> 0 SATS S58.SI 2.99 3.00 3.00 1815000 3.53 2.93 ## >>> 1 SGX S68.SI 7.18 7.19 7.18 1397000 7.63 6.66
To specify the parameters to be output, it can be changed in the following method of the script. In future, this will be refined to be more user friendly.
def form_cur_quotes_property_url_str(self): """ To form the properties/parameters of the data to be received for current quotes To eventually utilize the get_table_fr_xls. Current use default parameters. name(n0), symbol(s), the latest value(l1), open(o) and the close value of the last trading day(p) volumn (v), year high (k), year low(j) Further info can be found at : https://code.google.com/p/yahoo-finance-managed/wiki/enumQuoteProperty """ start_str = '&f=' target_properties = 'nsl1opvkj' self.cur_quotes_property_portion_url = start_str + target_properties
To download data from web, the following pattern method is used:
def downloading_csv(self, url_address): """ Download the csv information from the url_address given. """ url = URL(url_address) f = open(self.cur_quotes_csvfile, 'wb') # save as test.gif f.write(url.download()) f.close()
The full script can be found at GitHub.