The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Scrape daily stock prices into a spreadsheet

To scrape daily stock prices into a spreadsheet, you can use Python with libraries like yfinance for data and pandas for spreadsheet handling. Here’s a simple script that fetches stock prices and saves them into an Excel or CSV file:


Python Script to Scrape Daily Stock Prices into a Spreadsheet

python
import yfinance as yf import pandas as pd from datetime import datetime # List of stock tickers tickers = ['AAPL', 'GOOGL', 'MSFT', 'AMZN', 'TSLA'] # Get today's date today = datetime.today().strftime('%Y-%m-%d') # Download data data = yf.download(tickers, period='1d', interval='1d', group_by='ticker', auto_adjust=True) # Format data rows = [] for ticker in tickers: if ticker in data.columns.levels[0]: price_data = data[ticker].iloc[0] rows.append({ 'Date': today, 'Ticker': ticker, 'Open': price_data['Open'], 'High': price_data['High'], 'Low': price_data['Low'], 'Close': price_data['Close'], 'Volume': price_data['Volume'] }) # Convert to DataFrame df = pd.DataFrame(rows) # Save to Excel or CSV df.to_excel('daily_stock_prices.xlsx', index=False) # Alternatively: df.to_csv('daily_stock_prices.csv', index=False) print("Stock prices saved successfully.")

Requirements

Install the required libraries before running the script:

bash
pip install yfinance pandas openpyxl

Automating Daily Scraping

You can automate this script using:

  • Windows: Task Scheduler

  • macOS/Linux: cron jobs

Let me know if you need help setting that up or adjusting the script for Google Sheets or another platform.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About