CodeForFinance
Python

Web Scraping Financial Data with Python

Learn to scrape financial data from websites using Python, BeautifulSoup, and requests. Stock prices, news, and market data.

Getting Started

pip install requests beautifulsoup4 pandas

Core Implementation

import requests
from bs4 import BeautifulSoup
import pandas as pd

# Scrape stock data from a financial page
url = "https://finance.yahoo.com/quote/AAPL"
headers = {"User-Agent": "Mozilla/5.0"}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")

# Note: Yahoo Finance uses dynamic rendering
# For reliable data, use yfinance or an API instead
# This demonstrates the technique for static pages

# Example: scrape a table from a static page
tables = pd.read_html(response.text)
for i, table in enumerate(tables):
    print(f"Table {i}:")
    print(table.head())
    print()

Extended Example

# Better approach: scrape multiple pages with rate limiting
import time

tickers = ["AAPL", "MSFT", "GOOGL"]
results = []

for ticker in tickers:
    url = f"https://finance.yahoo.com/quote/{ticker}"
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        soup = BeautifulSoup(response.text, "html.parser")
        # Parse the data you need
        results.append({"ticker": ticker, "status": "scraped"})

    time.sleep(2)  # Be respectful - 2 second delay between requests

print(f"Scraped {len(results)} pages")

Developer Essentials

As an Amazon Associate we may earn from qualifying purchases.