Skip to content

Instantly share code, notes, and snippets.

@beefy
Last active October 4, 2016 20:34
Show Gist options
  • Save beefy/e1ce71e1abc58d3a8e94d30459248cb2 to your computer and use it in GitHub Desktop.
Save beefy/e1ce71e1abc58d3a8e94d30459248cb2 to your computer and use it in GitHub Desktop.
finance.yahoo.com web scrapper in python
#!/usr/bin/env python2.7
# taken from https://www.youtube.com/user/sentdex
import time
import urllib2
from urllib2 import urlopen
from lxml import html
sp500short = ['a', 'aa', 'aapl', 'abbv', 'abc', 'abt', 'ace', 'aci', 'acn', 'act', 'adbe', 'adi', 'adm', 'adp']
def yahooKeyStats(stock):
try:
url = 'http://finance.yahoo.com/q/ks?s='+stock
source = urllib2.urlopen(url).read()
tree = html.fromstring(source)
pbr = tree.xpath("//td[@class='yfnc_tabledata1']/text()")[5]
return float(pbr)
except Exception, e:
print stock + ' not found'
return -1
for stock in sp500short:
val = yahooKeyStats(stock)
if val < 0.70:
pass
print stock, val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment