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)