I am using the following program to pull data from yahoo finance into a text file on the local drive. The program does successfully pull the data from yahoo finance into a txt file in my computer.
Why do the code come up with 19 "error"'s followed by a successful pull?
If I want to save the data to database server, how should I approach?
import urllib.request
import time
stockstoPull = 'AMD', 'BAC', 'MSFT', 'TXN', 'GOOG'
def pullData(stock):
fileLine = stock + '.txt'
urltovisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
with urllib.request.urlopen(urltovisit) as f:
sourceCode = f.read(100000).decode('utf-8')
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split('.')
if len(splitLine) == 5:
if 'values' not in eachLine:
saveFile = open(fileLine,'a')
linetoWrite = eachLine+'\n'
saveFile.write(linetoWrite)
else:
print('Error')
print('Pulled', stock)
print('...')
time.sleep(.5)
for eachStock in stockstoPull:
pullData(eachStock)
The code is correct, I changed only 2 things. The errors are when the splitLine length is less than 6. You can use sqlite as a database.
import urllib.request
import time
stockstoPull = 'AMD', 'BAC', 'MSFT', 'TXN', 'GOOG'
def pullData(stock):
fileLine = stock + '.txt'
urltovisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
with urllib.request.urlopen(urltovisit) as f:
sourceCode = f.read().decode('utf-8')
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',') # <---(here ',' instead of '.')
if len(splitLine) == 6: # <----( here, 6 instead of 5 )
if 'values' not in eachLine:
saveFile = open(fileLine,'a')
linetoWrite = eachLine+'\n'
saveFile.write(linetoWrite)
print('Pulled', stock)
print('...')
time.sleep(.5)
if __name__=="__main__":
for eachStock in stockstoPull:
pullData(eachStock)