-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullStock.py
37 lines (30 loc) · 1017 Bytes
/
pullStock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 30 00:47:16 2014
@author: Pranav
"""
import urllib2
import time
import datetime
StocksToPull = 'ITC.NS','BHEL.NS','idfc.NS'
def pullData(stock):
try:
fileName = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10d/csv'
#alt = 'localhost/ITC.csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine) == 6:
if 'values' not in eachLine:
saveFile = open(fileName,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print "stock pulled", stock
print 'sleeping'
time.sleep(5)
except Exception,e:
print "main loop fail "+(e)
for eachStock in StocksToPull:
pullData(eachStock);