有很多情況下,我們有時不得不獲取博客網站甚至瀏覽器的財務數據或報表。允許我們收集其財務數據的著名瀏覽器之一是雅虎,實際上,當我們需要執行此任務時,有許多實例。在本教程中,我們將學習 Python 中的 yfinance 模塊,我們將學習如何使用該模塊從雅虎獲取財務數據,以及我們可以從中收集什麼樣的數據。
python 中的yfinance
模塊
yfinance 是 Python 中著名的模塊之一,用於收集在線數據,有了它,我們可以收集雅虎的財務數據。在 yfinance 模塊的幫助下,我們檢索和收集公司的財務信息(如財務比率等。)以及營銷數據的歷史。但是,在我們開始學習更多關於這個模塊及其實現以及應用之前,我們必須在我們的系統中安裝 yfinance 模塊(因為它不是 Python 中的內置模塊)。安裝過程完成後,我們將進入 yfinance 模塊的實施部分。
裝置
在本節中,我們將學習如何在系統中安裝 yfinance 模塊,以便將它導入程序並使用其功能。我們必須按照以下步驟在設備上安裝 yfinance 模塊:
第一步:打開設備的命令提示終端,找到系統中安裝 Python 的目錄(使用 mkdir)。
第二步:現在在終端寫下以下命令,用 pip 安裝程序安裝 yfinance 模塊:
pip install yfinance
第三步:當我們按回車鍵時,pip 安裝程序將開始在系統中定義的路徑上安裝 yfinance 模塊。
如我們所見,yfinance 模塊已成功安裝在我們的系統中,現在我們可以通過將其導入 Python 程序來開始使用它。
履行
現在,我們將在 Python 程序中使用 yfinance 模塊,這樣我們就可以使用它從雅虎收集財務數據,並了解它的實現。當我們在程序中使用 yfinance 模塊時,我們必須在函數內部將 ticker 作為參數傳遞(這裡,ticker 被稱為公司的 Ticker)。
注:股票代碼基本上是公司名稱的股票符號或唯一的一系列字母,它被分配來添加額外的安全層,用於交易目的。請看以下我們將在 yfinance 模塊中使用的 tickers 示例:
- 谷歌的股票代碼是“GOOGL”
- 臉書的股票代碼是“FB”
- 而且,Ticker 對於亞馬遜來說是“AMZN”等。
現在,讓我們在各種示例程序中使用 yfinance 模塊,這樣我們就可以很容易地理解它的功能和實現。查看以下示例程序,這些程序使用其中的 yfinance 模塊來收集數據:
例 1:
看看下面的 Python 程序,我們將從雅虎金融中檢索臉書的財務數據:
# Import yfinance module in the program
import yfinance as yahooFin
# Using ticker for the Facebook in yfinance function
retrFBInfo = yahooFin.Ticker("FB")
# Printing Facebook financial information in the output
print(retrFBInfo.info)
輸出:
{'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 來使用它的功能。然後,我們使用帶有 FB Ticker 功能的 yahooFin 從雅虎收集了臉書頁面的所有財務信息。在從雅虎的臉書頁面檢索到財務信息後,我們在程序執行時將這些信息打印在輸出中。
正如我們在輸出中看到的,我們已經將所有的財務數據打印成了一個完整的 Python 字典。
從雅虎收集財務關鍵指標:
除了從雅虎網站檢索財務信息(如我們在示例 1 中所做的),我們甚至可以收集各種財務關鍵指標。這些財務關鍵指標可以是市盈率、公司貝塔係數和公司部門等。,來自我們在上面第一個示例中打印的整個 Python 字典。讓我們使用 yfinance 模塊檢索財務關鍵指標。
例 2:
請看下面的 Python 程序,我們將使用 yfinance 模塊從雅虎檢索財務關鍵指標:
# Import yfinance module
import yfinance as yahooFin
# Using ticker for the Facebook in yfinance function
retrFBInfo = yahooFin.Ticker("FB")
# Getting Key metrics from the Facebook financial data we retrieved
comSecKey = retrFBInfo.info['sector'] # Company Sector key
keyPER = retrFBInfo.info['trailinPE'] # Price Earning ratio key
comBetaKey = retrFBInfo.info['beta'] # Company Beta key
# Print the Company Sector Information
print("The Company Sector Metric key we collected from the page: ", comSecKey)
# Print the Price Earnings Ratio (PER) from the FB page
print("The Price Earnings Ratio (PER) we got from the financial data of page: ", keyPER)
# Print the Company Beta metric from page
print("The Company Beta key we retrieved from page: ", comBetaKey)
輸出:
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),並從我們從臉書頁面收集的財務數據中存儲了財務關鍵指標的值。在本例中,我們檢索了市盈率、公司 Beta 和公司部門財務指標關鍵字,並將它們存儲在各自的變量中。存儲完財務指標鍵後,我們將它們打印在輸出中,正如我們在輸出中看到的,詳細信息已成功打印。
拆分財務指標鍵:
在第一個和第二個例子中,我們打印了雅虎的財務數據和指標關鍵字,因為它們是一些字典值。但是我們甚至可以通過鍵值對來拆分這些值,然後我們可以打印拆分結果。在下面的示例中,我們將通過拆分來打印財務指標鍵值,並通過循環來拆分它們。
例 3:
看看下面的 Python 程序,我們將把財務指標鍵分成鍵-值對:
# Import yfinance module
import yfinance as yahooFin
# Using ticker for the Facebook in yfinance function
retrFBInfo = yahooFin.Ticker("FB")
# Looping over items to split them in key-value pair
print("Items from the financial data of the Facebook page in the key-value page: ")
for keyItem, valueItem in retrFBInfo.info.items():
print(keyItem, ":", valueItem)
輸出:
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
說明:
從雅虎的臉書財務數據中檢索信息後,我們使用了 for
循環。在 for
循環的幫助下,我們已經循環了數據,然後我們將數據項分成鍵值對。最後,作為程序的結果,我們在輸出中打印了這些鍵值對。
檢索歷史市場價格:
我們甚至可以從雅虎的財務數據中檢索歷史市場價格,然後我們甚至可以在輸出中打印這些信息。
例 4:
讓我們首先看下面的例子,然後我們將了解更多關於檢索歷史市場價格的信息:
# Import yfinance module
import yfinance as yahooFin
# Using ticker for the Facebook in yfinance function
retrFBInfo = yahooFin.Ticker("FB")
# Initializing variable for retrieving market prices
maxHisMP = retrFBInfo.history(period="max")
# Printing the historical market prices in the output
print("Historical Market Prices data from the Facebook page financial data of Yahoo: ")
print(maxHisMP)
輸出:
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]
說明:
在這個例子中,我們從程序中檢索到的雅虎頁面中導入了臉書金融數據的歷史市場價格。我們已經使用帶有 FB Ticker 的 history()函數從臉書的金融數據中檢索了歷史市場價格,然後我們已經將信息存儲在我們已經初始化的變量(即 maxHisMP)中。最後,作為程序的結果,我們在輸出中打印了這個歷史市場價格信息。我們可以在雅虎財務數據中看到最長時期的市場價格歷史信息。
注意:我們在歷史函數中用 FB 跑馬燈定義的“最大值”用於收集雅虎最大時間段的市場價格歷史。max 關鍵字將檢索雅虎中臉書每日價格變化的歷史。
現在,我們已經了解了如何使用歷史()函數從雅虎的財務數據中獲取市場價格的歷史。但是有一件事我們甚至可以最小化我們從歷史中得到的數據來降低它的複雜性。我們可以通過在 history()函數中給出一個固定的時間參數來做到這一點,並且只從 Yahoo 中檢索少量數據。
在這裡,我們將通過以下兩種方式執行該功能:
1.收集自上次“1 天”或“5 天”或“1 個月”或“3 個月”以來的固定時間數據。,方法是在歷史函數中提供固定時間參數。讓我們通過在程序中使用固定時間參數,然後從雅虎檢索市場價格來理解這一點。
例 5:
請看下面的 Python 程序,我們將在其中檢索臉書最近 3 個月的市場價格歷史:
# Import yfinance module
import yfinance as yahooFin
# Using ticker for the Facebook in yfinance function
retrFBInfo = yahooFin.Ticker("FB")
# Initializing variable for retrieving market prices
his3MonMP = retrFBInfo.history(period="3mo")
# Printing the historical market prices in the output
print("Historical Market Prices data of last 3 months from the Facebook page of Yahoo: ")
print(his3MonMP)
輸出:
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]
說明:
我們使用了與前面示例程序相同的邏輯,並從雅虎的臉書頁面收集了歷史市場價格數據。但是,在這個程序中,我們沒有收集最大數據,而是通過在歷史函數中給出參數‘3mo’來限制我們的數據,這樣它將只檢索過去三個月的市場價格歷史。然後,我們在輸出中打印了過去三個月的市場價格歷史。
正如我們在輸出中看到的,雅虎臉書頁面的市場價格歷史僅在過去三個月才被打印出來。
注意:當我們必須從過去的固定時間收集數據時,我們不能在歷史函數中給出任何持續時間參數,因為它不是有效的參數。對於 yfinance 模塊的歷史函數,我們有以下有效的固定時間段參數:max、ytd、10y、5y、2y、1y、6mo、3mo、1mo、5d 和 1d。我們不能在 history()函數中給出任何其他時間段參數,因為它將被視為無效參數,並且不會檢索市場價格歷史數據。
2.通過給出開始時間和結束時間作為 history()函數的參數,從 history()函數中收集固定數據。我們可以通過定義開始和結束時間變量,然後使用這些變量作為 history()函數的參數,來檢索我們想要的時間段的數據。讓我們通過在程序中使用這些定義的時間參數,然後從雅虎檢索市場價格歷史來理解這一點。
例 6:
請看下面的 Python 程序,我們將在其中檢索用戶定義的時間段內臉書的市場價格歷史:
# Import yfinance module
import yfinance as yahooFin
# Importing datetime module in the program
import datetime
# Using ticker for the Facebook in yfinance function
retrFBInfo = yahooFin.Ticker("FB")
# Define a start date variable
strtDate = datetime.datetime(2021, 4, 26)
# Define an end date variable
endDate = datetime.datetime(2021, 8, 6)
# Initializing variable for retrieving market prices
fixTimeMP = retrFBInfo.history(start = strtDate, end = endDate)
# Printing the historical market prices in the output
print("Historical Market Prices data of the time period we defined from the financial data of Facebook page: ")
print(fixTimeMP)
輸出:
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()函數中使用這些變量作為從 Yahoo 檢索數據的開始和結束日期。我們在 history()中使用了 start 和 end 關鍵字來設置收集數據的開始日期和結束日期。這就是我們如何從雅虎檢索用戶固定時間的市場價格歷史。最後,我們在輸出中打印了最小化的數據。
我們可以在輸出中看到,我們已經從雅虎獲得了市場價格的固定時間(我們在程序中設置的)歷史。
結論
在本教程中,我們已經討論了如何在 Python 程序中使用 yfinance 模塊,通過使用模塊中給出的函數來簡單地檢索雅虎的財務數據或雅虎財務數據的各個方面,然後我們可以打印這些數據以供進一步使用。
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129444.html