Additional stock data such as company balance sheets and financial records can be scraped from yahoo finance website as described in the previous post. An alternative way which is much faster can be done using the Yahoo Query Language (YQL) . It provides collections of data from various sources including Yahoo finance data and enable easy query of specific data sets. The results is generated in the form of json format which itself can be easily retrieved from the url link generated from the YQL query.
The YQL provides a YQL console which provides easy way for users to key in the SQL syntax to query for particular information. For example, to obtain key company statistics such as P/E ratio, cash flow etc. The following SQL can be inputted into the console.
SELECT * FROM yahoo.finance.keystats WHERE symbol in ("N4E.SI","BS6.SI")
Pressing the “Test” button will generate a url that will link to the json file containing all the information. Example of the url string is as below.
https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20yahoo.finance.keystats%20WHERE%20symbol%20in%20(%22N4E.SI%22%2C%22BS6.SI%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
The url can now be used with the PATTERN module to download and processed the json information. For reading json file, simplejson module can be used. The url can be modified easily to include more stock symbols (the text highlighted in blue). For each url, I have included about 45 stocks symbols and loop it for all the stock symbols required. Users can also customize and filter the data using standard SQL syntax.
This method is much faster compared to the direct scraping method described previously as multiple stock symbols can be processed at one go and the json data can be easily retrieved. In contrast, direct scraping can only processed single web page (or stock) at one go and require handling of XPATH to get the data correctly.
The YQL contains 1000 of tables from different websites such as Flickr, wordpress, twitter etc and the data are easily organized in table form for easy retrieval. The url string also provides additional flexibility to query more data set.
The script for this can be easily done using standard url string formation, downloading of raw data using the Pattern module, reading the data using simplejson and converting the data to dataframe using Python Pandas.
One advantage of json file is that it is basically a dict file (of eg 45 stocks symbols) and a list of dict files can be easily transformed to a pandas Data frame for further processing. Below code abstract shows the portion in which the json file is being loaded and converted to a dict to append to a list. This list is in turn convert to Dataframe object by passing the list of dicts to the Dataframe object.
def get_datalist_fr_json(self): """ Set to self.com_data_allstock_list. Will keep appending without any reset. """ raw_data = json.load(open(self.saved_json_file, 'r')) for indivdual_set in raw_data['query']['results']['stats']: temp_dict_data = {} if type(indivdual_set) == str: #for single data continue # temp do not use for parameters in indivdual_set.keys(): if type(indivdual_set[parameters]) == str: temp_dict_data[parameters] = indivdual_set[parameters]#for symbol elif type(indivdual_set[parameters]) == dict: if indivdual_set[parameters].has_key('content'): temp_dict_data[parameters] = indivdual_set[parameters]['content'] ## append to list self.com_data_allstock_list.append(temp_dict_data) def get_com_data_fr_all_stocks(self): """ Cater for situation where there is large list. For safeguard, clip limit to 49. """ full_list = self.replace_special_characters_in_list(self.full_stocklist_to_retrieve) chunk_of_list = self.break_list_to_sub_list(self.full_stocklist_to_retrieve) self.temp_full_data_df = None for n in chunk_of_list: # print the progress sys.stdout.write('.') # set the small chunk of list self.set_target_stocks_list(n) self.get_com_data() # convert to dataframe self.com_data_allstock_df = pandas.DataFrame(self.com_data_allstock_list) self.com_data_allstock_df.rename(columns ={'symbol':'SYMBOL'}, inplace=True)
Hello!
Your blog is extremely helpful. Thank you.
I tried to follow your instructions on entering SELECT * FROM yahoo.finance.keystats WHERE symbol in (“N4E.SI”,”BS6.SI”) in YQL consol, but it returns the message “no definition found for Table yahoo.finance.keystats”. Do you know which table name I should use?
Thank you so much!
Jialu
Hi Jialu, I think this table no longer working/exist in YQL. For alternatives, you can consider scraping from google stock screener (link: https://simply-python.com/2015/09/26/simple-python-script-to-retrieve-all-stocks-data-from-google-finance-screener) or can try to scrape from Yahoo website directly (example: https://wordpress.com/post/simply-python.com/194)