Financial data api to filter stocks based on queries like market cap

475 Views Asked by At

Is there any way to filter the stocks from financial data apis based on specific queries like list of all companies over Market cap of 100 million in amy of the providers currently available? rather to give the specific list of quotes as input always. Am trying to check for any of the financial data apis available currently like Alpha vantage, Financial Modeling Prep but so far the samples I have come across always has the inputs as specific stock quotes names like APPL, MSFT etc. If I can get the fundamental data of all companies by Market cap in json then I can do the additional filtering in json file itself like checking PE ratio etc.

Thank you.

1

There are 1 best solutions below

2
ASH On

I can't get Spyder working so I can't test this, but the code below should work...I think...

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)