This project demonstrates how to scrape live stock prices from a financial website (e.g., Yahoo Finance) using Python libraries like requests
, BeautifulSoup
, and pandas
. The scraped data is saved to a CSV file for further analysis.
- Scrapes live stock prices from Yahoo Finance.
- Saves stock data in a CSV file format for easy analysis and storage.
- Flexible script that can be extended to scrape multiple stocks.
Make sure you have the following installed:
- Python 3.x
- Required libraries:
requests
beautifulsoup4
pandas
You can install the required libraries using:
pip install -r requirements.txt
Create a requirements.txt
file with the following content:
requests
beautifulsoup4
pandas
Run the following command to install all dependencies:
pip install -r requirements.txt
The project has a simple structure:
.
├── scraper.py
├── requirements.txt
└── README.md
scraper.py
: Contains the Python script that scrapes the stock price.requirements.txt
: Lists the required dependencies for this project.README.md
: The documentation file for the project.
In scraper.py
, import the required libraries:
import requests
from bs4 import BeautifulSoup
import pandas as pd
Use the requests
library to send an HTTP request to the stock price website. In this case, we will scrape stock data from Yahoo Finance.
url = "https://finance.yahoo.com/quote/AAPL?p=AAPL"
response = requests.get(url)
Use BeautifulSoup
to parse the HTML content and extract the necessary data (e.g., stock price).
soup = BeautifulSoup(response.text, 'html.parser')
price = soup.find('fin-streamer', {'data-field': 'regularMarketPrice'}).text
print(f"Apple Stock Price: ${price}")
Once the stock price is fetched, it can be saved into a CSV file for further usage.
data = {'Stock': ['AAPL'], 'Price': [price]}
df = pd.DataFrame(data)
df.to_csv('stock_prices.csv', index=False)
Run the scraper.py
file to fetch the latest stock price:
python scraper.py
This will scrape the current stock price of Apple (AAPL) and save it to a file named stock_prices.csv
.
Stock,Price
AAPL,150.23
You can customize this script to fetch prices for different stocks by modifying the URL and extracting different data fields. Replace the stock symbol in the URL and adjust the web scraping selectors accordingly.