试图获得一只股票(TGT)过去5年的盈利日期,以及2)当天伴随的价格百分比变化。然后将数据放入数据帧。
这是我发现到目前为止使用的代码,但似乎错误来自日期范围。在这种情况下,雅虎!金融使你把特定的日期和时间放在一个日期范围内,这是不理想的。如果有人知道怎么做的话,最好的办法就是有一些大致划定的5年期限等。非常感谢!
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发布于 2022-04-18 18:35:07
YahooEarningsCalendar不再被积极维护,最新的承诺可以追溯到2020年。
查看代码时,每当刮到的结果与预期的站点结构不匹配时,就会引发错误。对于请求中缺少的未决问题,存在几个User-Agents,它们现在是抓取雅虎财务网页所必需的(参见这个问题)。因此,站点结构不匹配。
不幸的是,您无法通过API手动设置此代理。我建议使用yahoo_fin作为插入替代。
你的代码会像这样。
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')发布于 2022-11-10 05:53:11
import yfinance as yf
import pandas as pd
stock = yf.Ticker('TGT')
df = stock.quarterly_earnings
or
df = stock.earnings要做到这一点,熊猫只被用来获取数据中的数据,这使得它更好地保存到csv或sql例如。
https://stackoverflow.com/questions/71914472
复制相似问题