我正在使用以下程序将yahoo finance中的数据提取到本地驱动器上的文本文件中。该程序成功地将yahoo finance中的数据提取到我计算机中的txt文件中。
为什么代码会出现19“错误”,然后成功拉动?
如果我想将数据保存到数据库服务器,我应该如何处理?
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)
答案 0 :(得分:2)
代码是正确的,我只改变了两件事。当splitLine长度小于6时,错误。您可以使用sqlite作为数据库。
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)
答案 1 :(得分:-2)
您可以找到tutorial来获取雅虎财务数据&#34;与熊猫。
Pandas,Python数据分析库,拥有自己的远程数据访问选项,可以立即将Yahoo Finance数据作为数据帧获取。
P.S。 :请注意页面顶部的警告。您需要另外安装一个库,即#p; pandas_datareader&#34;。查询的结果是一个pandas数据帧。