Skip to content

This project demonstrates how to scrape live stock prices from a financial website using Python libraries like requests, BeautifulSoup, and pandas. The scraped data is saved to a CSV file for further analysis.

License

Notifications You must be signed in to change notification settings

soroush-saki/Stock-Price-Scraper-Using-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Stock Price Scraper Using Python

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.

Features

  • 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.

Prerequisites

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

Requirements Setup

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

Project Structure

The project has a simple structure:

.
├── scraper.py
├── requirements.txt
└── README.md

File Descriptions:

  • 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.

Steps for Web Scraping

1. Import Necessary Libraries

In scraper.py, import the required libraries:

import requests
from bs4 import BeautifulSoup
import pandas as pd

2. Send HTTP Request

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)

3. Parse the HTML

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}")

4. Save Data to CSV

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)

Running the Script

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.

Example Output:

Stock,Price
AAPL,150.23

Customize for Different Stocks

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.

About

This project demonstrates how to scrape live stock prices from a financial website using Python libraries like requests, BeautifulSoup, and pandas. The scraped data is saved to a CSV file for further analysis.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages