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)
You can find a tutorial for getting "Yahoo Finance Data" with pandas.
Pandas, Python data analysis library, has it's own remote data access option for getting Yahoo Finance data immediately as a dataframe.
PS : Be aware of the warning on the top of the page. You need to install a library additionally which is "pandas_datareader". The result of the query comes a pandas dataframe.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.