python - 从 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()
解决方案
解决方案:您可以创建一个符号列表并将此列表提供给 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()
推荐阅读
- javascript - YouTube 视频 API 片段标题包含 Next.js 中的特殊字符
- java - 如何在 JOOQ 中公开新的 SQL 函数
- jenkins - 将 allure jenkins 插件与 jenkins.groovy 集成
- maven - 确保在没有项目父 pom 的所有其他模块之前构建 maven 模块
- python - 将字符串添加到两列数据框中的重复字符串
- php - mysql查询的对称差或析取联合
- java - Java - ClassNotFoundException: java.net.http.HttpClient
- python - 一些两个字母的序列重复了 3 次?“知足”和“维持”就是这样的词
- java - 正则表达式验证包含 '=' 和 '~' 作为结束字符的字符串
- c# - Xamarin.Forms Picker ItemsSource 在 XAML 中保持为空