In [3]:
from selenium import webdriver
import time
import csv

csvFile = "results.csv" #where to store results

## Ticker symbols to pull data for ##
tickers = [
    "CAB",
    "DKS"
]

## Statistics to pull from Yahoo Finance "Key Statistcs" Page 
keyStatistics = [
    "Market Cap (intraday)",
    "Forward P/E",
    "Revenue",
    "EBITDA",
    "Total Cash",
    "Total Debt",
    "Forward Annual Dividend Yield",
    "Payout Ratio",
    "52-Week Change",
    "Short % of Float",
   
]

def findTDValue(tds,tdRelatedText):
    # The pattern of the td we want
    pattern = './/span[text()="' + tdRelatedText + '"]'
    try:
        want = filter(lambda x: len(x.find_elements_by_xpath(pattern)) > 0, tds)
        # return the text of sibling td
        return [str(want[0].find_element_by_xpath('.//following-sibling::td').text)]
    except:
        return ["N/A"]

    
# Because the data is read by javascript, so I used selenium  

if __name__ == '__main__':


    browser = webdriver.Chrome()

    resultTable = [["Ticker"] + keyStatistics] #append ticker as first column of results

    for ticker in tickers: #iterate through tickers
        print "Scraping ticker " + ticker
        print "=" * 50
        page = 'http://finance.yahoo.com/q/ks?s='+ticker+'+Key+Statistics' #pull page data
        browser.get(page)
        # Sleep 2s to make sure the web browser render the whole page
        time.sleep(2)
        all_tds = browser.find_elements_by_xpath('//td')
        resultRow = [ticker] #start new result row with first item as the ticker symbol
        for keyStatistic in keyStatistics: #iterate through remaining statistic items, and append to row
            print "Looking for keyStatistic " + keyStatistic
            print "*" * 25
            resultRow += findTDValue(all_tds, keyStatistic)
        resultTable += [resultRow] #write entire row as new record in table

with open(csvFile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    writer.writerows(resultTable)


Scraping ticker CAB
==================================================
Looking for keyStatistic Market Cap (intraday)
*************************
Looking for keyStatistic Forward P/E
*************************
Looking for keyStatistic Revenue
*************************
Looking for keyStatistic EBITDA
*************************
Looking for keyStatistic Total Cash
*************************
Looking for keyStatistic Total Debt
*************************
Looking for keyStatistic Forward Annual Dividend Yield
*************************
Looking for keyStatistic Payout Ratio
*************************
Looking for keyStatistic 52-Week Change
*************************
Looking for keyStatistic Short % of Float
*************************
Scraping ticker DKS
==================================================
Looking for keyStatistic Market Cap (intraday)
*************************
Looking for keyStatistic Forward P/E
*************************
Looking for keyStatistic Revenue
*************************
Looking for keyStatistic EBITDA
*************************
Looking for keyStatistic Total Cash
*************************
Looking for keyStatistic Total Debt
*************************
Looking for keyStatistic Forward Annual Dividend Yield
*************************
Looking for keyStatistic Payout Ratio
*************************
Looking for keyStatistic 52-Week Change
*************************
Looking for keyStatistic Short % of Float
*************************

In [ ]: