不是我牛是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用户请点击浏览器图标
选择“Disable on www.wenxuecity.com”

安装Adblock用户请点击图标
选择“don't run on pages on this domain”