0

I'm trying to extract the number of the institutional holding that a stock has.

For instance, here from Apple at the following link

https://finance.yahoo.com/quote/AAPL/holders?p=AAPL)

Extract a number

I would like to get the number 5,102 from:

 "institutionsCount": {
    "raw": 5102,
    "fmt": "5.1k",
    "longFmt": "5,102"
}

I think this is a tulip right?

Code

This is the code so far:

import requests
from bs4 import BeautifulSoup

headers = {'user-agent': 'Mozilla/5.0'}

url = 'https://finance.yahoo.com/quote/AAPL/holders?p=AAPL'

r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')

What I tried

I tried to use yfinance but I don't know how to make a loop to extract all the holders for each ticker. So I though would be more easy to do a web scraping for one and then looping for each stock.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Santi Gil
  • 49
  • 3
  • 1
    Do you need beautifulsoup? - https://pypi.org/project/yfinance/ – OneCricketeer Feb 08 '22 at 16:29
  • 2
    And what do you mean by "tulip"? That's a flower – OneCricketeer Feb 08 '22 at 16:30
  • 1
    Did you do research, e.g. [example using `yfinance`](https://stackoverflow.com/questions/38967533/retrieve-company-name-with-ticker-symbol-input-yahoo-or-google-api/40244249#40244249) ? – hc_dev Feb 08 '22 at 16:35
  • I tried to use yfinance but I don't know how to make a loop to extract all the holders for each ticker. So I though would be more easy to do a web scraping for one and then looping for each stock. But apparently I was wrong :( – Santi Gil Feb 08 '22 at 16:46
  • 1
    @SantiGil web-scraping not only cumbersome, but also often forbidden by _Terms and Conditions_ or the [Robots-file](https://en.wikipedia.org/wiki/Robots_exclusion_standard). That's why developers spent effort in writing a module like `yfinance`. Present your attempts with yfinance and we can help you on the last-mile. But please [edit] your question directly. – hc_dev Feb 08 '22 at 21:55

2 Answers2

1

Using yfinance

Try the module yfinance (an API facade, client library to Yahoo! Finance). Some research here on SO shows how:

Then adjust to your case and play around:

import yfinance as yf

stock = yf.Ticker('APPL')
print(stock.info)

Prints the retrieved JSON data stock.info as a dict:

{'symbol': 'APPL', 'quoteType': 'MUTUALFUND', 'exchange': 'YHD', 'exchangeTimezoneName': 'America/New_York', 'exchangeTimezoneShortName': 'EST', 'gmtOffSetMilliseconds': '-18000000', 'market': 'us_market', 'isEsgPopulated': False, 'quoteSourceName': 'Delayed Quote', 'regularMarketOpen': None, 'averageDailyVolume3Month': None, 'regularMarketTime': 1561759658, 'volume24Hr': None, 'regularMarketDayHigh': None, 'shortName': None, 'averageDailyVolume10Day': None, 'longName': None, 'regularMarketChange': None, 'regularMarketPreviousClose': None, 'preMarketPrice': None, 'exchangeDataDelayedBy': 0, 'toCurrency': None, 'postMarketChange': None, 'postMarketPrice': None, 'exchangeName': 'YHD', 'preMarketChange': None, 'circulatingSupply': None, 'regularMarketDayLow': None, 'priceHint': 2, 'regularMarketPrice': None, 'regularMarketVolume': None, 'lastMarket': None, 'regularMarketSource': 'DELAYED', 'openInterest': None, 'marketState': 'POST', 'underlyingSymbol': None, 'marketCap': None, 'volumeAllCurrencies': None, 'strikePrice': None, 'maxAge': 1, 'fromCurrency': None, 'logo_url': ''}

However, there is no institution or any other count resembling your information-demand (given number 5,102) like on the screenshot from Yahoo! Finance page of AAPL: Yahoo! Finance page of AAPL, clipping with institutions count

Research on the desired figure

I searched the web for Number of Institutions Holding Shares and yfinance and found an article explaining it:

Ticker.major_holders and Ticker.institutional_holders can be used to get details of shareholding of the company.

major_holders: shows how much of the shares and float are held by the insiders and institutions

Ticker.institutional_holders provides the details of major institutioal shareholding.

Retrieve information about holders using yfinance

import yfinance as yf

appl = yf.Ticker('APPL')

full_name = appl.info['longName']
major_holders = appl.major_holders
institutional_holders = appl.institutional_holders

print(f"Name:\n{full_name}")
print(f"Major holders:\n{major_holders}")
print(f"Institutional holders:\n{institutional_holders}")

Prints what is expected to hold the desired number:

Name:
None
Major holders:
                         0   1
0           Previous Close NaN
1               YTD Return NaN
2      Expense Ratio (net) NaN
3                 Category NaN
4            Last Cap Gain NaN
5       Morningstar Rating NaN
6  Morningstar Risk Rating NaN
7    Sustainability Rating NaN
Institutional holders:
                      0   1
0            Net Assets NaN
1     Beta (5Y Monthly) NaN
2                 Yield NaN
3     5y Average Return NaN
4     Holdings Turnover NaN
5         Last Dividend NaN
6  Average for Category NaN
7        Inception Date NaN

Now it is left to you to figure out, why there is no data available for APPL.

Test also the ticker symbol AMZN and see if the example outputs from Medium article can be reproduced with figures available ️

Parsing JSON an extracting a value

Given you have this JSON:

 "institutionsCount": {
    "raw": 5102,
    "fmt": "5.1k",
    "longFmt": "5,102"
}

Then you can parse it using Pythons standard module json:

import json

json_text = """
 "institutionsCount": {
    "raw": 5102,
    "fmt": "5.1k",
    "longFmt": "5,102"
}
"""
obj = json.loads('{'+json_text+'}')  # surrounding braces needed to make it a valid JSON (object)
print(obj['institutionsCount']['longFmt'])
# 5,102
hc_dev
  • 8,389
  • 1
  • 26
  • 38
0

Thank you so much to all for the inspiration. As hc_dev mention, would be better to use the module of yfinance. Since yfinance gives you a pandas.core.frame, I transform it to a list, so I can grab what I wanted. So I think the following would solve the problem efficiently.

CODE

import yfinance as yf

ticker = input('Enter ticker:')
stock = yf.Ticker(ticker)
holders = stock.major_holders
holders_list = holders[0].to_list()
number = holders_list[3]
print('Number of institutionals:', number)
Santi Gil
  • 49
  • 3