Skip to content

Instantly share code, notes, and snippets.

@barvinograd
Last active December 23, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barvinograd/6592024 to your computer and use it in GitHub Desktop.
Save barvinograd/6592024 to your computer and use it in GitHub Desktop.
Yahoo! Historical Stock Prices Crawler. Downloads historical prices of stocks. Ticker lists is available from http://www.nasdaq.com/screening/company-list.aspx
#!/usr/bin/python
import urllib2
import datetime
from os.path import isfile, join
from os import getcwd
tFile = open("tickers.txt")
eYear = datetime.datetime.now().year
eMonth = datetime.datetime.now().month
eDay = datetime.datetime.now().day
for ticker in tFile:
ticker = ticker.strip()
if (isfile(join(getcwd(), ticker + ".csv"))):
continue
print join(getcwd(), ticker + ".csv")
url = "http://ichart.finance.yahoo.com/table.csv?s={0}&d={1}&e={2}&f={3}&g=d&a=1&b=1&c=1900&ignore=.csv".format(ticker, eMonth, eYear, eDay)
print "downloading data for ", ticker, "...",
try:
response = urllib2.urlopen(url)
data = response.read()
with open(ticker + ".csv", "w") as tData:
tData.write(data)
print " done."
except Exception as ex:
print " error occurred. " , ex, "."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment