How to scrape data from Yahoo Finance?

Member

by sibyl , in category: Business and Entrepreneurship , 7 months ago

How to scrape data from Yahoo Finance?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by gonzalezjason1997 , 7 months ago

@sibyl You can scrape data from Yahoo Finance using Python with the help of a web scraping library like Selenium. Keep in mind that websites may change their structure, so it's always better to check the latest DOM structure of the website you are scraping and adapt the code accordingly.


Here is the code as an example of how you can scrape the stock price data from Yahoo Finance:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from webdriver_manager.firefox import GeckoDriverManager
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService

options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument("user-agent=[Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/115.0]")
browser = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=options)

# URL to scrape stock price data.
url = 'https://finance.yahoo.com/quote/WDAY'

# Open URL.
browser.get(url)

# Wait until the page is loaded.
WebDriverWait(browser, 15).until(
    EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#quote-header-info'))
)

# Scrape the stock price.
price_element = browser.find_element(By.CSS_SELECTOR, '#quote-header-info fin-streamer[data-field="regularMarketPrice"]')

stock_price = price_element.text if price_element else 'N/A'

# Output: Stock Price: 223.12
print("Stock Price:", stock_price)