<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          超實用!教你用 Python 獲取并下載美股數(shù)據(jù)

          共 4830字,需瀏覽 10分鐘

           ·

          2022-05-25 05:04

          yfinance 是一個使用 Yahoo! 獲取數(shù)據(jù)的 Python 第三方模塊。它支持獲取最細(xì)到1分鐘級的歷史數(shù)據(jù)及股票基本面數(shù)據(jù),是免費獲得美股分鐘級及以上粒度數(shù)據(jù)的不二之選。


          1.準(zhǔn)備



          開始之前,你要確保Python和pip已經(jīng)成功安裝在電腦上,如果沒有,可以訪問這篇文章:超詳細(xì)Python安裝指南?進(jìn)行安裝。

          (可選1)?如果你用Python的目的是數(shù)據(jù)分析,可以直接安裝Anaconda:Python數(shù)據(jù)分析與挖掘好幫手—Anaconda,它內(nèi)置了Python和pip.

          (可選2)?此外,推薦大家用VSCode編輯器,它有許多的優(yōu)點:Python 編程的最好搭檔—VSCode 詳細(xì)指南。

          請選擇以下任一種方式輸入命令安裝依賴
          1. Windows 環(huán)境 打開 Cmd (開始-運行-CMD)。
          2. MacOS 環(huán)境 打開 Terminal (command+空格輸入Terminal)。
          3. 如果你用的是 VSCode編輯器 或 Pycharm,可以直接使用界面下方的Terminal.

          pip install?yfinance


          2.yfinance 基本使用



          通過yfinance你可以使用一樣命令下載任意美股股票的數(shù)據(jù),比如:

          import?yfinance as?yf

          # 單股
          data = yf.download("AAPL", start="2017-01-01", end="2017-04-30")
          # Open High Low Close Adj Close Volume
          # Date
          # 2017-01-03 28.950001 29.082500 28.690001 29.037500 27.257641 115127600
          # 2017-01-04 28.962500 29.127501 28.937500 29.004999 27.227135 84472400
          # 2017-01-05 28.980000 29.215000 28.952499 29.152500 27.365593 88774400
          # 2017-01-06 29.195000 29.540001 29.117500 29.477501 27.670671 127007600
          # 2017-01-09 29.487499 29.857500 29.485001 29.747499 27.924126 134247600
          # ......

          # 多股
          data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30",
          ???????????????????group_by="ticker")
          # AAPL ... SPY
          # Open High Low Close ... Low Close Adj Close Volume
          # Date ...
          # 2017-01-03 28.950001 29.082500 28.690001 29.037500 ... 223.880005 225.240005 205.509079 91366500
          # 2017-01-04 28.962500 29.127501 28.937500 29.004999 ... 225.610001 226.580002 206.731735 78744400
          # 2017-01-05 28.980000 29.215000 28.952499 29.152500 ... 225.479996 226.399994 206.567459 78379000
          # 2017-01-06 29.195000 29.540001 29.117500 29.477501 ... 225.899994 227.210007 207.306549 71559900
          # ......


          默認(rèn)是獲取天級別的數(shù)據(jù),如果你需要獲取分鐘級的,只需要添加interval參數(shù):

          import?yfinance as?yf

          # 單股
          data = yf.download("AAPL", start="2022-05-18", end="2022-05-23", interval="1m")
          print(data)

          # Open High Low Close Adj Close Volume
          # Datetime
          # 2022-05-17 12:00:00-04:00 148.000000 148.050003 147.839996 147.865005 147.865005 0
          # 2022-05-17 12:01:00-04:00 147.869507 147.919998 147.779999 147.889893 147.889893 123746
          # 2022-05-17 12:02:00-04:00 147.889999 147.929993 147.750000 147.907394 147.907394 92847
          # 2022-05-17 12:03:00-04:00 147.904999 147.929993 147.785004 147.839996 147.839996 79266
          # 2022-05-17 12:04:00-04:00 147.839996 147.895004 147.779999 147.860001 147.860001 58905
          # ......


          它支持的分鐘級參數(shù)有:1m,2m,5m,15m,30m,60m,90m 等等。

          此外還支持小時級和天線、周線、月線級別:1h,1d,5d,1wk,1mo,3mo 等等。

          獲取到的數(shù)據(jù)類型就是Dataframe,因此你還可以直接保存為csv文件:

          # 公眾號:Python 實用寶典
          import?yfinance as?yf

          data = yf.download("AAPL", start="2022-05-18", end="2022-05-23", interval="1m")
          data.to_csv("aapl_20220518_20220523.csv")
          # 保存到本地,命名為 aapl_20220518_20220523.csv

          3.通過yfinance獲取股票基本數(shù)據(jù)



          如果你需要獲取一只股票的基本數(shù)據(jù),如市值、市盈率、股息等,你可以通過定義一只股票的Ticker,利用其info屬性獲?。?/p>

          # 公眾號:Python 實用寶典
          import?yfinance as?yf

          aapl = yf.Ticker("aapl")
          print(aapl.info)
          # {'zip': '95014', 'sector': 'Technology', 'fullTimeEmployees': 154000, 'longBusinessSummary': '
          Apple ......


          這個字典比較長,這里省略顯示了,里面包含了比如市盈率(PE)等信息:

          # 公眾號:Python 實用寶典
          import?yfinance as?yf

          aapl = yf.Ticker("aapl")
          aapl.info['forwardPE']
          # 20.974085


          你還可以獲取每次派息數(shù)據(jù):

          # 公眾號:Python 實用寶典
          import?yfinance as?yf

          aapl = yf.Ticker("aapl")
          print(aapl.dividends)
          # Date
          # 1987-05-11 0.000536
          # 1987-08-10 0.000536
          # 1987-11-17 0.000714
          # 1988-02-12 0.000714
          # 1988-05-16 0.000714
          # ...
          # 2021-05-07 0.220000
          # ... ...


          獲取資產(chǎn)負(fù)債表:

          # 公眾號:Python 實用寶典
          import?yfinance as?yf

          aapl = yf.Ticker("aapl")
          print(aapl.balancesheet)
          # 2021-09-25 2020-09-26 2019-09-28 2018-09-29
          # Total Liab 2.879120e+11 2.585490e+11 2.480280e+11 2.585780e+11
          # Total Stockholder Equity 6.309000e+10 6.533900e+10 9.048800e+10 1.071470e+11
          # Other Current Liab 5.357700e+10 4.786700e+10 4.324200e+10 3.929300e+10
          # Total Assets 3.510020e+11 3.238880e+11 3.385160e+11 3.657250e+11
          # Common Stock 5.736500e+10 5.077900e+10 4.517400e+10 4.020100e+10
          # ......


          現(xiàn)金流數(shù)據(jù):

          # 公眾號:Python 實用寶典
          import?yfinance as?yf

          aapl = yf.Ticker("aapl")
          print(aapl.cashflow)
          # 2021-09-25 2020-09-26 2019-09-28 2018-09-29
          # Investments -2.819000e+09 5.335000e+09 5.809300e+10 3.084500e+10
          # Change To Liabilities 1.400200e+10 -1.981000e+09 -2.548000e+09 9.172000e+09
          # Total Cashflows From Investing Activities -1.454500e+10 -4.289000e+09 4.589600e+10 1.606600e+10
          # ......


          新聞數(shù)據(jù):

          # 公眾號:Python 實用寶典
          import?yfinance as?yf

          aapl = yf.Ticker("aapl")
          print(aapl.news)
          # [{'uuid': '476a41c6-c6dc-3050-9b8f-c3777c8485b2', 'title': "Dow Jones Futures Rise After 'Hard' Reality Hits Market; What To Do Now", 'publisher': "Investor's Business Daily",
          # 'link': 'https://finance.yahoo.com/m/476a41c6-c6dc-3050-9b8f-c3777c8485b2/dow-jones-futures-rise-after.html',
          # 'providerPublishTime': 1653305573, 'type': 'STORY'}, {'uuid': '721d466d-5394-3f3c-a9c3-b0920d44c7f3' ......


          總之,有了yfinance這個神器,除了高頻數(shù)據(jù)你無法獲取之外,其他的美股數(shù)據(jù)你都能獲取得到,有需要的小伙伴可以試試,非常好用。

          我們的文章到此就結(jié)束啦,如果你喜歡今天的Python 實戰(zhàn)教程,請持續(xù)關(guān)注Python實用寶典。

          有任何問題,可以在公眾號后臺回復(fù):加群,回答相應(yīng)紅字驗證信息,進(jìn)入互助群詢問。

          原創(chuàng)不易,希望你能在下面點個贊和在看支持我繼續(xù)創(chuàng)作,謝謝!

          點擊下方閱讀原文可獲得更好的閱讀體驗

          Python實用寶典?(pythondict.com)
          不只是一個寶典
          歡迎關(guān)注公眾號:Python實用寶典

          瀏覽 143
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  网站首页操B的视頻 | 国产AV大片 | 91二区三区| 久久国产V一级毛多内射 | 中文天堂在线观看 |