【发布时间】:2021-05-04 03:23:52
【问题描述】:
我正在尝试使用 Python 从 Yahoo Finance 上的 Sustainalytics 抓取历史 ESG 数据。具体来说,假设我想要给定成分表最近 10 年的 ESG 分数。
以下代码行提供了最新的 ESG 分数。但我想了解过去的 ESG 表现。我基本上是在寻找从 2010 年 1 月到 2020 年 12 月的年度(如果可能的话,每月)ESG。我想自动抓取并将数据保存在 txt 或 csv 文件中。
# import yfinance, pandas and os
import yfinance as yf
import pandas as pd
import os
单个代码的代码:
msft = "MSFT"
msft_y = yf.Ticker(msft)
esg_data = pd.DataFrame.transpose(msft_y.sustainability)
esg_data['company_ticker'] = str(msft_y ticker)
它返回一个包含 Microsoft 的 ESG 相关信息的 27 行数据框。
标准普尔 500 指数代码:
# Import list of tickers from file
os.chdir("C:\...")
sp_500 = pd.read_csv('SP_500_tickers.csv')
# Retrieve Yahoo! Finance Sustainability Scores for each ticker
for i in sp_500['ticker_code']:
# print(i)
i_y = yf.Ticker(i)
try:
if i_y.sustainability is not None:
temp = pd.DataFrame.transpose(i_y.sustainability)
temp['company_ticker'] = str(i_y.ticker)
# print(temp)
esg_data = esg_data.append(temp)
except IndexError:
pass
它返回 S&P500 成分股的 ESG 数据数据框,我们可以将其用于分析。背后的想法是创建“好”和“坏” ESG 公司的投资组合,并比较业绩,以了解股价在不同历史时期的表现。
到目前为止,这些代码无法获取过去日期的 ESG 数据。
【问题讨论】:
-
yfinance 是否附带有关历史时期的文档?
标签: python pandas web-scraping yahoo-finance