不是我牛是ChatGPT牛。。。

來源: slow_quick 2024-01-08 12:18:40 [] [博客] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (13773 bytes)
本文內容已被 [ slow_quick ] 在 2024-01-08 12:20:55 編輯過。如有問題,請報告版主或論壇管理刪除.
回答: 真牛!bobpainting2024-01-08 12:04:04

問:how to get company name and sector for a stock using yfinance

答:

To get the company name and sector for a stock using the yfinance library in Python, you can follow these steps:

  1. 1. Install yfinance if you haven't already:
  2. pip install yfinance
  3.  
  4. 2. Use the following Python code to get the company name and sector:
  5.  

import yfinance as yf

def get_company_info(ticker_symbol):
    # Create a Ticker object for the given stock symbol
    ticker = yf.Ticker(ticker_symbol)

    try:
        # Get the info dictionary for the stock
        info = ticker.info

        # Extract company name and sector from the info dictionary
        company_name = info['longName']
        sector = info['sector']

        return company_name, sector
    except Exception as e:
        print(f"Error: Unable to retrieve information for {ticker_symbol}.")
        print(f"Error details: {e}")
        return None, None

# Example: Get information for Apple Inc. (AAPL)
ticker_symbol = 'AAPL'
company_name, sector = get_company_info(ticker_symbol)

if company_name and sector:
    print(f"Company Name: {company_name}")
    print(f"Sector: {sector}")

所有跟帖: 

我隻是順藤摸瓜改了幾個地方 -slow_quick- 給 slow_quick 發送悄悄話 slow_quick 的博客首頁 (0 bytes) () 01/08/2024 postreply 12:21:59

怪不得這麽火,功能很強大。你用的很好 -bobpainting- 給 bobpainting 發送悄悄話 (0 bytes) () 01/08/2024 postreply 12:24:34

你也牛!我看不懂程序 -bobpainting- 給 bobpainting 發送悄悄話 (0 bytes) () 01/08/2024 postreply 12:22:48

請您先登陸,再發跟帖!

發現Adblock插件

如要繼續瀏覽
請支持本站 請務必在本站關閉/移除任何Adblock

關閉Adblock後 請點擊

請參考如何關閉Adblock/Adblock plus

安裝Adblock plus用戶請點擊瀏覽器圖標
選擇“Disable on www.wenxuecity.com”

安裝Adblock用戶請點擊圖標
選擇“don't run on pages on this domain”