Price to Book ratio example



This tutorial shows a quick example of how we might begin to pull company information on stocks. There are many sources for stock data online, though Yahoo has pretty much always stuck around, and provided free data.

Here, we're just pulling the price to book ratio for any company we pass through the function parameter.

import time
import urllib2
from urllib2 import urlopen

def yahooKeyStats(stock):
    try:
        sourceCode = urllib2.urlopen('http://finance.yahoo.com/q/ks?s='+stock).read()
        pbr = sourceCode.split('Price/Book (mrq):</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]
        print 'price to book ratio:',stock,pbr

    except Exception,e:
        print 'failed in the main loop',str(e)
		
		

The next tutorial:





  • Programming for Fundamental Investing
  • Getting Company Data
  • Price to Book ratio example
  • Python Stock Screener for Price to Book
  • Python Screener for PEG Ratio
  • Adding Price to Earnings
  • Getting all Russell 3000 stock tickers
  • Getting all Russell 3000 stock tickers part 2
  • More stock Screening
  • Completing Basic Stock Screener
  • Connecting with Quandl for Annual Earnings Data
  • Organizing Earnings Data
  • Graphing Finance Data
  • Finishing the Graphing
  • Adding the Graphing to the Screener
  • Preparing figure to Accept Finance Data
  • Adding Historical Earnings to Stock Screener Chart Data
  • Completing the Fundamental Investing Stock Screeners