从YahooFinance获取价格数据的原因:AttributeError'nonetype'对象没有属性'text'

2024-10-03 02:36:57 发布

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

我有一个循环(运行约200次)从YahooFinance获得以前的收盘价。此循环在某个点随机停止,并显示以下错误消息:

WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
[...]
AttributeError 'nonetype' object has no attribute 'text'

每次我运行脚本时,它都会在不同的点停止。以下是脚本:

from yahoofinancials import YahooFinancials
import csv

with open('instruments.csv', 'r') as csvfile:
    instruments = csv.reader(csvfile, delimiter=',', quoting = csv.QUOTE_NONNUMERIC, quotechar='"')
    for instrument in instruments:
        symbol = instrument[0]
        yahoo_financials = YahooFinancials(symbol)
        price = yahoo_financials.get_prev_close_price()

Tags: csvcsvfileimport脚本错误withsymbolprice
1条回答
网友
1楼 · 发布于 2024-10-03 02:36:57

解决方案:您可以创建一个符号列表,将此列表交给YahooFincials api,然后执行请求,而不是循环遍历每个符号并请求价格。看起来这个包裹可以很好地处理这个问题,虽然需要一些时间。 以下是doc的摘录:

from yahoofinancials import YahooFinancials
tech_stocks = ['AAPL', 'MSFT', 'INTC']
yahoo_financials_tech = YahooFinancials(tech_stocks)
tech_stock_price_data = yahoo_financials_tech.get_prev_close_price()

相关问题 更多 >