Python yfinance 模块2025年3月17日 | 阅读13分钟 很多时候我们需要获取一个博客网站甚至浏览器的财务数据或报表。雅虎是允许我们收集其财务数据的著名浏览器之一,而且确实有很多时候我们需要执行此任务。在本教程中,我们将学习 Python 的 yfinance 模块,以及如何使用该模块从雅虎获取财务数据以及可以收集的数据类型。 Python 中的 yfinance 模块yfinance 是 Python 中一个著名的模块,用于收集在线数据,我们可以从中收集雅虎的财务数据。借助 yfinance 模块,我们可以使用其函数检索和收集公司的财务信息(如财务比率等)以及市场数据历史。但是,在我们开始学习有关此模块及其实现和应用程序的更多信息之前,我们必须在系统中安装 yfinance 模块(因为它不是 Python 的内置模块)。安装过程完成后,我们将继续进行 yfinance 模块的实现部分。 安装在本节中,我们将学习如何在我们的系统中安装 yfinance 模块,以便我们可以将其导入程序并使用其函数。我们必须按照以下步骤在设备上安装 yfinance 模块 步骤 1:打开设备上的命令提示符终端,并使用 `mkdir` 定位 Python 在系统中安装的目录。 步骤 2:现在,在终端中输入以下命令,使用 pip 安装程序安装 yfinance 模块 ![]() 步骤 3:当我们按 Enter 键时,pip 安装程序将开始在指定路径的系统中安装 yfinance 模块。 ![]() 正如我们所见,yfinance 模块已成功安装在我们的系统中,现在我们可以通过将其导入 Python 程序来开始使用它。 实施现在,我们将在 Python 程序中使用 yfinance 模块,以便我们可以使用它从雅虎收集财务数据并了解其实现。当我们在程序中使用 yfinance 模块时,我们必须在函数中将 Ticker 作为参数传递(此处,Ticker 指的是公司的 Ticker)。 注意:Ticker basically is a stock symbol or unique series of alphabets for the company names, and it is assigned to add an extra security layer for trading purposes. Look at the following examples of tickers that we will use in the yfinance module
现在,让我们在各种示例程序中使用 yfinance 模块,以便我们可以轻松理解其功能和实现。查看以下使用 yfinance 模块在其中收集数据的示例程序 示例 1 查看以下 Python 程序,其中我们将从 Yahoo Finance 中检索 Facebook 的财务数据 输出 {'zip': '94025', 'sector': 'Communication Services', 'fullTimeEmployees': 63404, 'longBusinessSummary': "Facebook, Inc. develops products that enable people to connect and share with friends and family through mobile devices, personal computers, virtual reality headsets, and in-home devices worldwide. The company's products include Facebook that enables people to connect, share, discover, and communicate with each other on mobile devices and personal computers; Instagram, a community for sharing photos, videos, and private messages; Messenger, a messaging application for people to connect with friends, family, groups, and businesses across platforms and devices; and WhatsApp, a messaging application that is used by people and businesses to communicate in a private way. It also provides Facebook Reality Labs, an augmented and virtual reality product that help people feel connected, anytime, and anywhere. Facebook, Inc. was founded in 2004 and is headquartered in Menlo Park, California.", 'city': 'Menlo Park', 'phone': '650-543-4800', 'state': 'CA', 'country': 'United States', 'companyOfficers': [], 'website': 'http://investor.fb.com', 'maxAge': 1, 'address1': '1601 Willow Road', 'industry': 'Internet Content & Information', 'ebitdaMargins': 0.49698, 'profitMargins': 0.37175998, 'grossMargins': 0.80977, 'operatingCashflow': 49358000128, 'revenueGrowth': 0.556, 'operatingMargins': 0.42523, 'ebitda': 52079001600, 'targetLowPrice': 225, 'recommendationKey': 'buy', 'grossProfits': 69273000000, 'freeCashflow': 22740000768, 'targetMedianPrice': 425, 'currentPrice': 330.05, 'earningsGrowth': 1.006, 'currentRatio': 5.425, 'returnOnAssets': 0.17950001, 'numberOfAnalystOpinions': 50, 'targetMeanPrice': 417.22, 'debtToEquity': 9.089, 'returnOnEquity': 0.31332, 'targetHighPrice': 500, 'totalCash': 64079998976, 'totalDebt': 12563000320, 'totalRevenue': 104789999616, 'totalCashPerShare': 22.728, 'financialCurrency': 'USD', 'revenuePerShare': 36.82, 'quickRatio': 5.095, 'recommendationMean': 1.9, 'exchange': 'NMS', 'shortName': 'Facebook, Inc.', 'longName': 'Facebook, Inc.', 'exchangeTimezone 说明 我们首先在程序中将 yfinance 模块导入为 `yahooFin` 以使用其功能。然后,我们使用了 `yahooFin` 和 `FB Ticker` 函数来收集雅虎 Facebook 页面的所有财务信息。从雅虎 Facebook 页面检索财务信息后,我们将在程序执行时在输出中打印这些信息。 正如我们在输出中看到的,我们已将所有财务数据作为一个完整的 Python 字典打印出来。 从雅虎收集关键财务指标除了从雅虎网站检索财务信息(如我们在示例 1 中所做的那样),我们还可以收集各种关键财务指标。这些关键财务指标可以是市盈率、公司 Beta 和公司行业等,来自我们在上面第一个示例中打印的完整 Python 字典。让我们使用 yfinance 模块检索关键财务指标。 示例 2 查看以下 Python 程序,其中我们将使用 yfinance 模块从雅虎检索关键财务指标 输出 The Company Sector Metric key we collected from the page: Communication Services The Price Earnings Ratio (PER) we got from the financial data of page: 26.211199 The Company Beta key we retrieved from page: 1.295305 说明 在本程序中,我们没有检索完整的财务数据信息,而是仅检索了有限的关键财务指标。我们首先初始化了三个变量(`keyPer`、`comSecKey` 和 `comBetaKey`),并将关键财务指标的值存储在我们从 Facebook 页面收集的财务数据中。在此示例中,我们检索了市盈率、公司 Beta 和公司行业财务指标键,并将它们分别存储在相应的变量中。存储关键财务指标后,我们在输出中将它们打印为结果,正如我们在输出中看到的,详细信息已成功打印。 拆分关键财务指标在第一个和第二个示例中,我们像字典值一样打印了雅虎的财务数据和指标键。但我们也可以通过键值对拆分这些值,然后打印拆分结果。在下面的示例中,我们将通过拆分来打印关键财务指标值,并且我们将通过循环遍历它们来拆分它们。 示例 3 查看以下 Python 程序,其中我们将关键财务指标拆分为键值对 输出 Items from the financial data of the Facebook page in the key-value page: zip : 94025 sector : Communication Services fullTimeEmployees : 63404 longBusinessSummary : Facebook, Inc. develops products that enable people to connect and share with friends and family through mobile devices, personal computers, virtual reality headsets, and in-home devices worldwide. The company's products include Facebook that enables people to connect, share, discover, and communicate with each other on mobile devices and personal computers; Instagram, a community for sharing photos, videos, and private messages; Messenger, a messaging application for people to connect with friends, family, groups, and businesses across platforms and devices; and WhatsApp, a messaging application that is used by people and businesses to communicate in a private way. It also provides Facebook Reality Labs, an augmented and virtual reality product that help people feel connected, anytime, and anywhere. Facebook, Inc. was founded in 2004 and is headquartered in Menlo Park, California. city : Menlo Park phone : 650-543-4800 state : CA country : United States companyOfficers : [] website : http://investor.fb.com maxAge : 1 address1 : 1601 Willow Road industry : Internet Content & Information ebitdaMargins : 0.49698 profitMargins : 0.37175998 grossMargins : 0.80977 operatingCashflow : 49358000128 revenueGrowth : 0.556 operatingMargins : 0.42523 ebitda : 52079001600 说明 从雅虎检索 Facebook 的财务数据信息后,我们对其使用了 `for` 循环。借助 `for` 循环,我们遍历了数据,然后将数据项拆分为键值对。最后,我们将这些键值对作为程序的结果打印在输出中。 检索历史市场价格我们甚至可以从雅虎财务数据中检索历史市场价格,然后我们也可以在输出中打印这些信息。 示例 4 让我们先看下面的例子,然后我们将更深入地了解检索历史市场价格 输出 Historical Market Prices data from the Facebook page financial data of Yahoo: Open High Low Close Volume \ Date 2012-05-18 42.049999 45.000000 38.000000 38.230000 573576400 2012-05-21 36.529999 36.660000 33.000000 34.029999 168192700 2012-05-22 32.610001 33.590000 30.940001 31.000000 101786600 2012-05-23 31.370001 32.500000 31.360001 32.000000 73600000 2012-05-24 32.950001 33.209999 31.770000 33.029999 50237200 ... ... ... ... ... ... 2021-10-04 335.529999 335.940002 322.700012 326.230011 42885000 2021-10-05 328.579987 335.179993 326.160004 332.959991 35377900 2021-10-06 329.739990 334.380005 325.799988 333.640015 26443000 2021-10-07 337.000000 338.839996 328.980011 329.220001 28307500 2021-10-08 331.510010 333.399994 328.709991 330.049988 15934300 Dividends Stock Splits Date 2012-05-18 0 0 2012-05-21 0 0 2012-05-22 0 0 2012-05-23 0 0 2012-05-24 0 0 ... ... ... 2021-10-04 0 0 2021-10-05 0 0 2021-10-06 0 0 2021-10-07 0 0 2021-10-08 0 0 [2364 rows x 7 columns] 说明 在此示例中,我们从程序中检索到的雅虎页面中的 Facebook 财务数据导入了历史市场价格。我们使用 `history()` 函数和 `FB Ticker` 检索了 Facebook 财务数据的历史市场价格,然后我们将信息存储在已初始化的变量 `maxHisMP` 中。最后,我们将此历史市场价格信息作为程序结果打印在输出中。我们可以看到有关雅虎财务数据中可用历史市场价格的信息。 注意:我们在 `history` 函数中与 `FB ticker` 结合使用的 `max` 用于收集雅虎中可用的最长时间段的市场价格历史。`max` 关键字将检索 Facebook 在雅虎中可用的每日价格变动历史。现在,我们已经了解了如何使用其 `history()` 函数从雅虎的财务数据中获取市场价格的历史记录。但是,有一件事是我们可以最小化从 `history()` 获取的数据以降低其复杂性。我们可以通过在 `history()` 函数中提供固定的时间参数来做到这一点,并从雅虎检索少量数据。 在这里,我们将通过以下两种方式执行此功能 1. 通过在 `history` 函数中提供固定时间参数来收集自 last '1 day' 或 '5 day' 或 '1 month' 或 '3 month' 等的固定时间数据。让我们通过在程序中使用固定时间参数来了解这一点,然后从雅虎检索市场价格。 示例 5 查看以下 Python 程序,其中我们将检索 Facebook 过去 3 个月(`3mo`)的市场价格历史 输出 Historical Market Prices data of last 3 months from the Facebook page of Yahoo: Open High Low Close Volume \ Date 2021-07-09 345.320007 350.709991 345.269989 350.420013 13018700 2021-07-12 351.230011 354.190002 349.640015 353.160004 10018600 2021-07-13 351.500000 358.489990 348.799988 352.089996 11456000 2021-07-14 354.559998 355.200012 346.730011 347.630005 13894200 2021-07-15 349.230011 349.230011 340.220001 344.459991 14781600 ... ... ... ... ... ... 2021-10-04 335.529999 335.940002 322.700012 326.230011 42885000 2021-10-05 328.579987 335.179993 326.160004 332.959991 35377900 2021-10-06 329.739990 334.380005 325.799988 333.640015 26443000 2021-10-07 337.000000 338.839996 328.980011 329.220001 28307500 2021-10-08 331.510010 333.399994 328.709991 330.049988 15934300 Dividends Stock Splits Date 2021-07-09 0 0 2021-07-12 0 0 2021-07-13 0 0 2021-07-14 0 0 2021-07-15 0 0 ... ... ... 2021-10-04 0 0 2021-10-05 0 0 2021-10-06 0 0 2021-10-07 0 0 2021-10-08 0 0 [65 rows x 7 columns] 说明 我们使用了与上一个示例程序相同的逻辑,并从雅虎的 Facebook 页面收集了历史市场价格数据。但是,在此程序中,我们没有收集最大数据,而是通过在 `history` 函数中提供 `'3mo'` 参数来限制了数据,以便它只检索过去三个月的历史市场价格。然后,我们将这过去三个月的市场价格历史作为结果打印在输出中。 正如我们在输出中看到的,只打印了雅虎 Facebook 页面自过去三个月以来的市场价格历史。 注意:当我们必须从过去固定的时间收集数据时,我们不能在 `history` 函数中给出任何持续时间参数,因为它将不是有效参数。对于 yfinance 模块的 `history` 函数,我们有以下有效的固定时间段参数:`max`、`ytd`、`10y`、`5y`、`2y`、`1y`、`6mo`、`3mo`、`1mo`、`5d` 和 `1d`。我们不能在 `history()` 函数中给出任何其他时间段参数,因为它将被视为无效参数,并且不会检索任何市场价格历史数据。2. 通过将开始时间和结束时间作为 `history()` 函数的参数来从 `history()` 函数中收集固定数据。通过定义开始和结束时间变量,然后将这些变量用作 `history()` 函数的参数,我们可以检索我们想要的时间段的数据。让我们通过在程序中使用这些定义的 time 参数来了解这一点,然后从雅虎检索市场价格历史。 示例 6 查看以下 Python 程序,其中我们将检索 Facebook 在用户定义的期间的市场价格历史 输出 Historical Market Prices data of the time period we defined from the financial data of Facebook page: Open High Low Close Volume \ Date 2021-04-26 303.339996 305.799988 301.559998 303.040009 16172600 2021-04-27 304.279999 305.339996 301.109985 303.570007 15309300 2021-04-28 307.359985 310.920013 305.369995 307.100006 33907200 2021-04-29 330.119995 331.809998 321.609985 329.510010 56526800 2021-04-30 326.140015 329.820007 324.500000 325.079987 26332400 ... ... ... ... ... ... 2021-07-30 354.000000 360.730011 352.940002 356.299988 15966700 2021-08-02 358.100006 359.399994 350.739990 351.950012 13180400 2021-08-03 352.730011 353.769989 347.700012 351.239990 12406100 2021-08-04 352.420013 360.480011 351.510010 358.920013 14180600 2021-08-05 359.640015 363.899994 356.899994 362.970001 10247200 Dividends Stock Splits Date 2021-04-26 0 0 2021-04-27 0 0 2021-04-28 0 0 2021-04-29 0 0 2021-04-30 0 0 ... ... ... 2021-07-30 0 0 2021-08-02 0 0 2021-08-03 0 0 2021-08-04 0 0 2021-08-05 0 0 [72 rows x 7 columns] 说明 我们在此程序中导入了 `yfinance` 以及 `datetime` 模块,以便我们可以使用其 `datetime()` 函数。然后,我们定义了两个变量,其中我们使用 `datetime` 模块的 `datetime` 函数设置了开始日期和结束日期。然后,我们将这些变量用作 `history()` 函数中的开始和结束日期,从雅虎检索数据。我们在 `history()` 中使用了 `start` 和 `end` 关键字来设置收集数据的开始日期和结束日期。这样,我们就可以从雅虎检索用户自定义时间段的市场价格历史。最后,我们将最小化数据作为结果打印在输出中。 我们在输出中可以看到,我们获得了雅虎上我们(在程序中设置的)固定时间段的市场价格历史。 结论在本教程中,我们讨论了如何在 Python 程序中使用 yfinance 模块来简单地检索雅虎的财务数据或雅虎财务数据的各种方面,通过使用模块中提供的函数,然后我们可以打印这些数据以供进一步使用。 |
我们请求您订阅我们的新闻通讯以获取最新更新。