Scraping Yahoo Finance Historical Stock Data using Selenium

Programmer’s Picnic · Learn with Champak Roy
📦 Code on GitHub Tip: open GitHub in a new tab to avoid leaving the lesson.

Why requests Fails for Yahoo Finance

Yahoo Finance loads the historical price table using JavaScript. When we use requests, Python only downloads the initial HTML — it does not execute JavaScript.

So the historical table you see in the browser is missing in the downloaded HTML.

📌 Rule of Thumb:
If data appears only after the page loads → Selenium (or browser automation) is required.

Why Selenium Works

Selenium opens a real browser (Chrome), executes JavaScript, and lets us interact with dynamically loaded content.

Working Python Code (Page Source)

This version prints the fully loaded HTML (driver.page_source). Next step is extracting the History table rows.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep

# ---------- INPUT ----------
stockurl = "https://finance.yahoo.com/quote/RELIANCE.NS/history"

# ---------- DRIVER ----------
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

try:
    driver.get(stockurl)

    # Wait for JavaScript-loaded content
    sleep(5)

    # ---------- DATA ----------
    html = driver.page_source
    print(html)

finally:
    # Always close the browser safely
    driver.quit()

What This Program Does

  1. Opens Yahoo Finance History page for a stock
  2. Waits for JavaScript to load the content
  3. Captures the final HTML
  4. Prints it (so we can confirm the table exists)
🎓 This is a common first step in finance scraping pipelines: verify the HTML contains the data, then parse it into a table.

Next Upgrades