Scraping Central is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Guide

How to Scrape Stock Market Data

Learn how to scrape stock prices, financial data, and market information using Python. Covers Yahoo Finance, APIs, and alternative data sources.

Stock market data scraping powers trading algorithms, financial analysis, and portfolio tracking. Here is how to get stock data programmatically.

Method 1: yfinance Library (Recommended Start)

The yfinance library wraps Yahoo Finance data in a clean Python API.

import yfinance as yf

ticker = yf.Ticker("AAPL")
hist = ticker.history(period="1mo")
print(hist[["Open", "High", "Low", "Close", "Volume"]])

info = ticker.info
print(f"Market Cap: {info['marketCap']}")
print(f"P/E Ratio: {info['trailingPE']}")

Pros: Free, easy to use, covers most stocks and ETFs. Cons: Unofficial API, may break without notice. Rate limited.

Method 2: Scraping Financial Websites

For data not available through yfinance, scrape financial sites directly.

import requests
from bs4 import BeautifulSoup

API_KEY = "YOUR_SCRAPERAPI_KEY"
url = "https://finance.yahoo.com/quote/TSLA/"

resp = requests.get(
    f"http://api.scraperapi.com?api_key={API_KEY}&url={url}&render=true"
)
soup = BeautifulSoup(resp.text, "html.parser")

Using ScraperAPI is recommended here because financial sites heavily use JavaScript rendering and anti-bot protection.

Method 3: Official APIs

Several providers offer free or paid stock data APIs:

Provider Free Tier Real-Time Historical
Alpha Vantage 25 req/day Yes Yes
Polygon.io Limited Yes Yes
Twelve Data 800 req/day Yes Yes
IEX Cloud Pay-as-you-go Yes Yes

What Data to Collect

  • Price data, Open, high, low, close, volume (OHLCV)
  • Fundamentals, P/E ratio, market cap, earnings
  • News and sentiment, Headlines affecting stock price
  • Options data, Call/put chains, implied volatility
  • Insider transactions, SEC filings

Best Practices

  1. Start with yfinance for basic price and fundamental data
  2. Use official APIs for production applications that need reliability
  3. Use ScrapingAnt or ScraperAPI when you need to scrape financial websites directly
  4. Cache data aggressively, historical prices do not change
  5. Be aware of data licensing, redistributing real-time market data often requires paid licenses
  6. Handle market hours, Stock data only updates during trading hours