尝试使用Python 3.5从Yahoo Finance中提取股票数据

时间:2016-06-11 06:59:20

标签: python

您好我有这段代码,但是我收到了这些错误

主循环'元组'对象没有属性'读' AND 主循环模块' urllib'没有属性' urlopen'

def pullData(stock):
    try:
        fileLine = stock+'.txt'
        urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10d/csv'
        sourceCode = urllib.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(fileLine,'a')
                    lineToWrite = eachLine+'\n'
                    saveFile.write(lineToWrite)

        print('Pulled',stock)
        print('sleeping')
        time.sleep(5)

    except Exception as e:
        print('main loop',str(e))

pullData(stockToPull)

1 个答案:

答案 0 :(得分:0)

在Python 3中,from urllib.request import urlopen sourceCode = urlopen(urlToVisit).read() 位于urllib.request,因此您可以执行此操作:

var myObj = [

    {'name': 'Mike', 'number' : 'b1' , 'level' : 0 },
    {'name': 'Tom',  'number' : 'b2' , 'level' : 0 }
];

关于代码的其余部分,您最好使用HTML解析器,例如BeautifulSouplxml.html