首页 > 解决方案 > 从 YahooFinance 获取价格数据原因:AttributeError 'nonetype' object has no attribute 'text'

问题描述

我有一个循环(运行大约 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()

标签: pythonerror-handlingyahoo-financenonetype

解决方案


解决方案:您可以创建一个符号列表并将此列表提供给 YahooFincials api,然后执行请求,而不是遍历每个符号并请求价格。似乎这个包可以完美地处理这个问题,尽管它需要一些时间。这是文档的摘录:

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()

推荐阅读