尝试使用Python3.5从YahooFinance获取股票数据

2024-09-25 12:35:15 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我有这个代码,但得到这些错误

主循环“tuple”对象没有属性“read”主循环模块“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)

Tags: inread属性stockurlliburlopensplitprint
1条回答
网友
1楼 · 发布于 2024-09-25 12:35:15

在Python 3中,urlopen()位于^{}中,因此可以执行以下操作:

from urllib.request import urlopen

sourceCode = urlopen(urlToVisit).read()

对于剩下的代码,最好使用一个HTML解析器,比如BeautifulSoup或{a3}。在

相关问题 更多 >