【问题标题】:Market cap of all tickers in stock exchanges using python使用python的证券交易所所有股票代码的市值
【发布时间】:2022-06-17 10:42:07
【问题描述】:

我需要获取证券交易所(纽约证券交易所和纳斯达克)所有股票代码的当前市值以及它们的其他信息,例如收盘价、开盘价等(但这些是可选的)。

有没有像 csv 文件或 api 请求这样的方法可以给我这些信息?

我认为yfinance api没有这个功能来返回所有股票的市值,对吧?

【问题讨论】:

  • 整个纽约证券交易所的市值为27.7万亿美元。整个纳斯达克的市值为 23 万亿美元。在这方面,Google 比 Python 快得多。
  • 我认为this question has already been answered on SO。只需获取代码列表并循环播放即可。或者,如果您只想将所有这些结合起来,请遵循 Tim 的建议并使用 Google。
  • @TimRoberts 我需要这些交易所中每个股票代码的市值,而不是交易所的总市值。
  • @ramzeek,我认为在那个问题中,有一个包含所有代码和信息的文件,它没有说明文件的来源。
  • 我敢肯定,您只需通过几次 Google 搜索即可完成所有工作。

标签: python stock yfinance


【解决方案1】:

使用像 Financial Modeling Prep 这样的 API。

import pandas as pd
import requests

FMP_key = 'YOUR API KEY HERE'

# Define lists of symbols and market caps
symbols = []
market_caps = []

# Define a function to get the url based on your API key and 
def url(symbol: str, api_key):
    return "https://financialmodelingprep.com/api/v3/market-capitalization/" + symbol + "?apikey=" + api_key

# All the symbols you want the data for
all_symbols = ['AAPL', 'MSFT', 'NFLX', 'NVDA', 'FB', 'TWTR', 'TSLA', 'AMZN', 'WMT']

# DataFrame to store all the data
data = pd.DataFrame(columns = ['Symbol', 'Market Cap'])

# Iterate based on the symbols list and add to DataFrame
for item in all_symbols:
    response = requests.get(url(symbol = item, api_key = FMP_key)).json()

    symbols.append(response[0]['symbol'])
    market_caps.append(response[0]['marketCap'])
    
# Add data to the dataframe
data['Symbol'] = symbols
data['Market Cap'] = market_caps

结果:

【讨论】:

    【解决方案2】:

    以下代码示例对您有用吗?如果没有,我有几个替代示例脚本。

    import requests
    from bs4 import BeautifulSoup
    from pandas import DataFrame
    import itertools
    import numpy as np
    from itertools import chain
    
    url_base = "https://finviz.com/quote.ashx?t="
    
    tckr = ['MSFT','AAPL','AMZN']
            
    i = 1
    
    url_list = [(s, url_base + s) for s in tckr]
    data_list = []
    
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0'}
    
    for t, url in url_list:
        print(i)
        i = i + 1
        print(t, url)
        print('Scrapping ticker {}...'.format(t))
        soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')
        #writer.writerow([t])
        l = []
        for row in soup.select('.snapshot-table2 tr'):
            l.append([td.text for td in row.select('td')])
            x = list(chain.from_iterable(l))
            d = dict(zip(x[::2], x[1::2]))
            d['Index'] = t
            
        data_list.append(d)
    

    -- 结果:

    Out[139]: 
    [{'Index': 'MSFT',
      'P/E': '25.56',
      'EPS (ttm)': '9.59',
      'Insider Own': '0.07%',
      'Shs Outstand': '7.49B',
      'Perf Week': '-7.49%',
      'Market Cap': '1917.59B',
      'Forward P/E': '22.80',
      'EPS next Y': '15.54%',
      'Insider Trans': '-0.56%',
      'Shs Float': '7.46B',
      'Perf Month': '-8.19%',
      'Income': '72.46B',
      'PEG': '1.59',
      'EPS next Q': '2.31',
      'Inst Own': '71.80%',
      'Short Float': '0.62%',
      'Perf Quarter': '-17.02%',
      'Sales': '192.56B',
      'P/S': '9.96',
      'EPS this Y': '39.70%',
      'Inst Trans': '-0.62%',
      'Short Ratio': '1.43',
      'Perf Half Y': '-26.80%',
      'Book/sh': '21.74',
      'P/B': '11.27',
      'ROA': '21.40%',
      'Target Price': '360.65',
      'Perf Year': '-6.11%',
      'Cash/sh': '13.37',
      'P/C': '18.32',
      'EPS next 5Y': '16.11%',
      'ROE': '47.00%',
      '52W Range': '241.51 - 349.67',
      'Perf YTD': '-27.16%',
      'Dividend': '2.48',
      'P/FCF': '41.75',
      'EPS past 5Y': '25.70%',
      'ROI': '28.20%',
      '52W High': '-29.94%',
      'Beta': '0.94',
      'Dividend %': '1.01%',
      'Quick Ratio': '1.90',
      'Sales past 5Y': '13.00%',
      'Gross Margin': '68.70%',
      '52W Low': '1.43%',
      'ATR': '8.55',
      'Employees': '181000',
      'Current Ratio': '2.00',
      'Sales Q/Q': '18.40%',
      'Oper. Margin': '42.50%',
      'RSI (14)': '36.90',
      'Volatility': '2.69% 2.85%',
      'Optionable': 'Yes',
      'Debt/Eq': '0.40',
      'EPS Q/Q': '9.10%',
      'Profit Margin': '37.60%',
      'Rel Volume': '1.03',
      'Prev Close': '251.76',
      'Shortable': 'Yes',
      'LT Debt/Eq': '0.38',
      'Earnings': 'Apr 26 AMC',
      'Payout': '24.50%',
      'Avg Volume': '32.16M',
      'Price': '244.97',
      'Recom': '1.70',
      'SMA20': '-6.47%',
      'SMA50': '-10.19%',
      'SMA200': '-18.62%',
      'Volume': '33,061,518',
      'Change': '-2.70%'},
     {'Index': 'AAPL',
      'P/E': '21.14',
      'EPS (ttm)': '6.15',
      'Insider Own': '0.07%',
      'Shs Outstand': '16.28B',
      'Perf Week': '-8.82%',
      'Market Cap': '2189.63B',
      'Forward P/E': '19.84',
      'EPS next Y': '6.83%',
      'Insider Trans': '-1.58%',
      'Shs Float': '16.17B',
      'Perf Month': '-12.85%',
      'Income': '101.94B',
      'PEG': '2.13',
      'EPS next Q': '1.16',
      'Inst Own': '59.70%',
      'Short Float': '0.70%',
      'Perf Quarter': '-19.03%',
      'Sales': '386.02B',
      'P/S': '5.67',
      'EPS this Y': '71.40%',
      'Inst Trans': '-0.43%',
      'Short Ratio': '1.19',
      'Perf Half Y': '-27.46%',
      'Book/sh': '4.14',
      'P/B': '31.42',
      'ROA': '28.90%',
      'Target Price': '188.92',
      'Perf Year': '-1.31%',
      'Cash/sh': '3.06',
      'P/C': '42.51',
      'EPS next 5Y': '9.91%',
      'ROE': '152.90%',
      '52W Range': '128.46 - 182.94',
      'Perf YTD': '-26.76%',
      'Dividend': '0.92',
      'P/FCF': '24.05',
      'EPS past 5Y': '22.00%',
      'ROI': '50.00%',
      '52W High': '-28.91%',
      'Beta': '1.20',
      'Dividend %': '0.71%',
      'Quick Ratio': '0.90',
      'Sales past 5Y': '11.10%',
      'Gross Margin': '43.30%',
      '52W Low': '1.24%',
      'ATR': '5.23',
      'Employees': '154000',
      'Current Ratio': '0.90',
      'Sales Q/Q': '8.60%',
      'Oper. Margin': '30.90%',
      'RSI (14)': '33.79',
      'Volatility': '2.78% 3.31%',
      'Optionable': 'Yes',
      'Debt/Eq': '1.78',
      'EPS Q/Q': '9.20%',
      'Profit Margin': '26.40%',
      'Rel Volume': '1.13',
      'Prev Close': '135.43',
      'Shortable': 'Yes',
      'LT Debt/Eq': '1.53',
      'Earnings': 'Apr 28 AMC',
      'Payout': '14.20%',
      'Avg Volume': '95.49M',
      'Price': '130.06',
      'Recom': '1.90',
      'SMA20': '-8.73%',
      'SMA50': '-15.20%',
      'SMA200': '-18.27%',
      'Volume': '107,659,951',
      'Change': '-3.97%'},
     {'Index': 'AMZN',
      'P/E': '50.13',
      'EPS (ttm)': '2.07',
      'Insider Own': '9.80%',
      'Shs Outstand': '10.18B',
      'Perf Week': '-10.75%',
      'Market Cap': '1117.20B',
      'Forward P/E': '38.52',
      'EPS next Y': '238.07%',
      'Insider Trans': '-0.00%',
      'Shs Float': '457.72M',
      'Perf Month': '-10.15%',
      'Income': '21.41B',
      'PEG': '1.24',
      'EPS next Q': '0.16',
      'Inst Own': '60.70%',
      'Short Float': '21.70%',
      'Perf Quarter': '-34.07%',
      'Sales': '477.75B',
      'P/S': '2.34',
      'EPS this Y': '54.90%',
      'Inst Trans': '0.20%',
      'Short Ratio': '1.15',
      'Perf Half Y': '-40.19%',
      'Book/sh': '13.16',
      'P/B': '7.88',
      'ROA': '5.40%',
      'Target Price': '178.13',
      'Perf Year': '-40.58%',
      'Cash/sh': '6.16',
      'P/C': '16.83',
      'EPS next 5Y': '40.50%',
      'ROE': '16.90%',
      '52W Range': '101.26 - 188.65',
      'Perf YTD': '-37.82%',
      'Dividend': '-',
      'P/FCF': '-',
      'EPS past 5Y': '67.60%',
      'ROI': '9.50%',
      '52W High': '-45.05%',
      'Beta': '1.24',
      'Dividend %': '-',
      'Quick Ratio': '0.70',
      'Sales past 5Y': '28.10%',
      'Gross Margin': '42.10%',
      '52W Low': '2.37%',
      'ATR': '5.57',
      'Employees': '1622000',
      'Current Ratio': '1.00',
      'Sales Q/Q': '7.30%',
      'Oper. Margin': '4.10%',
      'RSI (14)': '37.30',
      'Volatility': '4.17% 4.11%',
      'Optionable': 'Yes',
      'Debt/Eq': '0.53',
      'EPS Q/Q': '-147.80%',
      'Profit Margin': '4.50%',
      'Rel Volume': '0.95',
      'Prev Close': '107.67',
      'Shortable': 'Yes',
      'LT Debt/Eq': '0.51',
      'Earnings': 'Apr 28 AMC',
      'Payout': '0.00%',
      'Avg Volume': '86.54M',
      'Price': '103.66',
      'Recom': '1.70',
      'SMA20': '-8.46%',
      'SMA50': '-18.17%',
      'SMA200': '-33.06%',
      'Volume': '81,975,496',
      'Change': '-3.72%'}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 2021-10-28
      相关资源
      最近更新 更多