是的,您可以使用Yahoo Finance API与Pandas库结合来提取当前或过去的股息数据。Yahoo Finance提供了一个可以直接通过URL访问的API接口,而Pandas是一个强大的数据处理和分析库,非常适合处理金融数据。
Yahoo Finance API允许用户通过HTTP请求获取股票市场的各种数据,包括股息信息。Pandas库则提供了DataFrame对象,这是一种二维表格型数据结构,非常适合于金融数据的存储和处理。
Yahoo Finance API可以提供多种类型的股息数据,包括:
以下是一个使用Python和Pandas库通过Yahoo Finance API获取股息数据的示例代码:
import pandas as pd
import requests
def get_dividends(symbol, start_date, end_date):
url = f"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?period1={start_date}&period2={end_date}&interval=1d&events=div"
response = requests.get(url)
data = response.json()
dividends = []
for event in data['chart']['result'][0]['events']['div']:
dividends.append({
'date': pd.to_datetime(event['date']),
'amount': event['amount']
})
return pd.DataFrame(dividends)
# 使用示例
symbol = 'AAPL' # 苹果公司的股票代码
start_date = int(pd.Timestamp('2020-01-01').timestamp())
end_date = int(pd.Timestamp('2023-01-01').timestamp())
dividends_df = get_dividends(symbol, start_date, end_date)
print(dividends_df)
通过上述方法,您可以有效地提取并分析股息数据,为您的投资决策提供支持。
领取专属 10元无门槛券
手把手带您无忧上云