Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- import pandas as pd
- import matplotlib.pyplot as plt
- def scrape_stock_data(stock_symbol):
- url = f'https://finance.yahoo.com/quote/{stock_symbol}'
- # Use a headless browser for dynamic content
- options = webdriver.ChromeOptions()
- options.add_argument('--headless')
- driver = webdriver.Chrome(options=options)
- driver.get(url)
- try:
- # Wait for the stock price to load (adjust timeout if needed)
- stock_price_element = WebDriverWait(driver, 10).until(
- EC.presence_of_element_located((By.CSS_SELECTOR, 'span[data-reactid*="50"]'))
- # You may need to modify the CSS selector based on your findings
- )
- stock_price = stock_price_element.text
- return {'stock_symbol': stock_symbol, 'stock_price': stock_price}
- except Exception as e:
- print(f"Failed to find stock price for {stock_symbol}: {e}")
- return None
- finally:
- driver.quit()
- def analyze_stock_data(data_frame):
- # Perform statistical analysis using pandas
- # Example: Calculate the mean, standard deviation
- mean_price = data_frame['stock_price'].mean()
- std_dev_price = data_frame['stock_price'].std()
- return mean_price, std_dev_price
- def visualize_data(data_frame):
- # Example: Create a simple bar plot
- data_frame.plot(x='stock_symbol', y='stock_price', kind='bar', title='Stock Prices')
- plt.show()
- def main():
- stock_symbols = ['AAPL', 'GOOGL', 'MSFT'] # Add more symbols as needed
- stock_data_list = []
- for symbol in stock_symbols:
- stock_data = scrape_stock_data(symbol)
- if stock_data:
- stock_data_list.append(stock_data)
- # Create a pandas DataFrame from the collected data
- data_frame = pd.DataFrame(stock_data_list)
- # Check if the DataFrame is not empty before proceeding
- if not data_frame.empty:
- # Analyze and visualize the data
- mean_price, std_dev_price = analyze_stock_data(data_frame)
- print(f"Mean Stock Price: {mean_price}")
- print(f"Standard Deviation of Stock Price: {std_dev_price}")
- visualize_data(data_frame)
- else:
- print("No data available.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement