首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用雅虎!为某只股票获得前5年收益日期的财务

使用雅虎!为某只股票获得前5年收益日期的财务
EN

Stack Overflow用户
提问于 2022-04-18 16:31:49
回答 2查看 365关注 0票数 0

试图获得一只股票(TGT)过去5年的盈利日期,以及2)当天伴随的价格百分比变化。然后将数据放入数据帧。

这是我发现到目前为止使用的代码,但似乎错误来自日期范围。在这种情况下,雅虎!金融使你把特定的日期和时间放在一个日期范围内,这是不理想的。如果有人知道怎么做的话,最好的办法就是有一些大致划定的5年期限等。非常感谢!

代码语言:javascript
复制
import datetime
from yahoo_earnings_calendar import YahooEarningsCalendar

date_from = datetime.datetime.strptime(
    'Jan 1 2017  10:00AM', '%b %d %Y %I:%M%p')
date_to = datetime.datetime.strptime(
    'Jan 1 2022  1:00PM', '%b %d %Y %I:%M%p')
yec = YahooEarningsCalendar()
# print(yec.earnings_on(date_from))
# print(yec.earnings_between(date_from, date_to))

print(yec.get_earnings_of('tgt'))

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\anaconda3\envs\fintech\lib\site-packages\yahoo_earnings_calendar\scraper.py in get_earnings_of(self, symbol)
    153         try:
--> 154             page_data_dict = self._get_data_dict(url)
    155             return page_data_dict["context"]["dispatcher"]["stores"]["ScreenerResultsStore"]["results"]["rows"]

~\anaconda3\envs\fintech\lib\site-packages\yahoo_earnings_calendar\scraper.py in _get_data_dict(self, url)
     37         page_content = page.content.decode(encoding='utf-8', errors='strict')
---> 38         page_data_string = [row for row in page_content.split(
     39             '\n') if row.startswith('root.App.main = ')][0][:-1]

IndexError: list index out of range

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-40-7c9d8ed0a796> in <module>
      1 # print(yec.get_earnings_date('tgt'))
----> 2 print(yec.get_earnings_of('tgt'))

~\anaconda3\envs\fintech\lib\site-packages\yahoo_earnings_calendar\scraper.py in get_earnings_of(self, symbol)
    155             return page_data_dict["context"]["dispatcher"]["stores"]["ScreenerResultsStore"]["results"]["rows"]
    156         except:
--> 157             raise Exception('Invalid Symbol or Unavailable Earnings Date')
    158 
    159 if __name__ == '__main__':  # pragma: no cover

Exception: Invalid Symbol or Unavailable Earnings Date
EN

回答 2

Stack Overflow用户

发布于 2022-04-18 18:35:07

YahooEarningsCalendar不再被积极维护,最新的承诺可以追溯到2020年。

查看代码时,每当刮到的结果与预期的站点结构不匹配时,就会引发错误。对于请求中缺少的未决问题,存在几个User-Agents,它们现在是抓取雅虎财务网页所必需的(参见这个问题)。因此,站点结构不匹配。

不幸的是,您无法通过API手动设置此代理。我建议使用yahoo_fin作为插入替代。

你的代码会像这样。

代码语言:javascript
复制
import yahoo_fin.stock_info as si

# alternative 1
ticker_earnings = si.get_earnings('TGT')
# alternative 2
earnings_in_range = si.get_earnings_in_date_range('2017-01-01','2022-01-01')
票数 1
EN

Stack Overflow用户

发布于 2022-11-10 05:53:11

代码语言:javascript
复制
import yfinance as yf
import pandas as pd
stock = yf.Ticker('TGT')
df = stock.quarterly_earnings
or
df = stock.earnings

要做到这一点,熊猫只被用来获取数据中的数据,这使得它更好地保存到csv或sql例如。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71914472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档