Python Scraping Stock Prices from Yahoo Finance
Scraping stock prices from Yahoo Finance is a common task for data analysis or web scraping projects. There are different ways to do it using Python, such as using requests and BeautifulSoup libraries, using yfinance library, or using Scrapingdog API.
Here is an example of using requests and BeautifulSoup libraries to scrape stock prices from Yahoo Finance:
# Import libraries
import requests
from bs4 import BeautifulSoup
# Define the URL of the page to scrape
url = "https://finance.yahoo.com/quote/AAPL?p=AAPL"
# Make a GET request to the URL
response = requests.get(url)
# Parse the HTML content with BeautifulSoup
soup = BeautifulSoup(response.text, "html.parser")
# Find the span element that contains the stock price
price = soup.find("span", class_="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)")
# Print the stock price
print(price.text)
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment