Selenium Downloader

Scraping Yahoo Finance Historical Stock Data using Selenium | Programmer’s Picnic

Scraping Yahoo Finance Historical Stock Data using Selenium

Programmer’s Picnic · Learn with Champak Roy

Why Requests Fails for Yahoo Finance

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

As a result, the historical data table is missing.

📌 Rule of Thumb:
If data appears only after the page loads → Selenium is required.

Why Selenium Works

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

  • ✔ JavaScript execution
  • ✔ Dynamic tables
  • ✔ Real-world scraping

Working Python Code (Yahoo Finance History)

Code on GIT


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


# ---------- INPUT ----------

stockurl = "https://finance.yahoo.com/quote/RELIANCE.NS/"

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

driver.get(stockurl)

# Wait for JavaScript-loaded content
sleep(5)

# ---------- Download Data ----------
data = driver.page_source

driver.quit()
print(data)
driver.quit()

What This Program Does

  1. Takes a stock name as input
  2. Opens Yahoo Finance historical page
  3. Waits for JavaScript to load data
  4. Extracts historical prices row by row
  5. Prints clean tabular output
🎓 This is a real industry scraping pattern used in finance, analytics, and ML pipelines.

Next Upgrades

  • Export data to CSV
  • Load into Pandas DataFrame
  • Plot stock price graphs
  • Headless browser mode
  • GUI-based stock scraper

0 Comments

Post a Comment

0 Comments

⬅ Go Home