python - Code not saving files, but no errors -
i trying run code pull in data yahoo finance. don't errors yet can't find files anywhere on computer. can help?
thanks, josh
this i'm using now, , i'm getting lot of 'oh no's. tried using number 5 , number 6. still nothing:
import urllib2 import time stockstopull = 'cjes','bp','msft','tsla','goog' def pulldata(stock): fileline = '/users/josh/documents/python'+stock+'.txt' urltovisit ='http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv' sourcecode = urllib2.urlopen(urltovisit).read() splitsource = sourcecode.split('\n') 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('oh no') print('pulled', stock) print('...') time.sleep(.5) eachstock in stockstopull: pulldata(eachstock)
fixed, helped.
i found bug in code. believe if len(splitline) == 6:
should if len(splitline) == 5:
for example if go to: http://chartapi.finance.yahoo.com/instrument/1.0/cjes/chartdata;type=quote;range=1y/csv
and read first line try split: 20120904,19.2400,20.3900,19.1200,20.1500,901600
it splits 5 element list using delimiter '.'. 20120904,19 2400,20 3900,19 1200,20 1500,901600
i modified code run in python 3.3.2 , worked me , pulled stock data. before made change if len(splitline) == 5:
code not entering first if
statement because len(splitline)
never 6.
here code worked me. note python 3.3 had change things make work should work using same logic of changing len(splitline)
thing.
import urllib.request import time stockstopull = 'cjes','bp','msft','tsla','goog' def pulldata(stock): fileline = stock+'.txt' urltovisit ='http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv' urllib.request.urlopen(urltovisit) f: sourcecode = f.read(100000).decode('utf-8') splitsource = sourcecode.split('\n') 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('oh no') print('pulled', stock) print('...') time.sleep(.5) eachstock in stockstopull: pulldata(eachstock)
Comments
Post a Comment