├── README.md └── python-webscrape.py /README.md: -------------------------------------------------------------------------------- 1 | # python-web-scraping 2 | Learning the web scrapping using python 3 | -------------------------------------------------------------------------------- /python-webscrape.py: -------------------------------------------------------------------------------- 1 | import yfinance as yf 2 | import datetime 3 | import random 4 | import math 5 | import pandas as pd 6 | from termcolor import colored 7 | 8 | def get_stock_data(ticker): 9 | stock = yf.Ticker(ticker) 10 | hist = stock.history(period="1mo") 11 | return hist 12 | 13 | def apply_lambda_functions(data): 14 | # Lambda to calculate daily percentage change 15 | percentage_change = data.apply(lambda row: ((row['Close'] - row['Open']) / row['Open']) * 100, axis=1) 16 | 17 | # Lambda to find day with the highest volume 18 | highest_volume_day = data['Volume'].idxmax() 19 | 20 | return percentage_change, highest_volume_day 21 | 22 | def math_operations(data): 23 | mean_close = data['Close'].mean() 24 | std_dev_close = data['Close'].std() 25 | max_close = data['Close'].max() 26 | min_close = data['Close'].min() 27 | # Find the dates for max and min closing prices 28 | date_max_close = data[data['Close'] == max_close].index[0] 29 | date_min_close = data[data['Close'] == min_close].index[0] 30 | 31 | return mean_close, std_dev_close, max_close, min_close, date_max_close, date_min_close 32 | 33 | def trading_days(data): 34 | return len(data) 35 | 36 | def latest_trading_date(data): 37 | return data.index[-1] 38 | 39 | def random_number_generator(data): 40 | random_date = random.choice(data.index) 41 | random_price = data.loc[random_date, 'Close'] 42 | return random_date, random_price 43 | 44 | # Example Usage 45 | ticker = 'AAPL' 46 | stock_data = get_stock_data(ticker) 47 | 48 | percentage_change, highest_volume_day = apply_lambda_functions(stock_data) 49 | mean_close, std_dev_close, max_close, min_close, date_max_close, date_min_close = math_operations(stock_data) 50 | trading_days_count = trading_days(stock_data) 51 | last_trading_date = latest_trading_date(stock_data) 52 | random_date, random_price = random_number_generator(stock_data) 53 | 54 | 55 | 56 | 57 | 58 | print(colored(f"Stock Data for {ticker}:", 'blue')) 59 | print(stock_data.head()) 60 | 61 | print(colored("\nDaily Percentage Change:", 'green')) 62 | print(percentage_change.head()) 63 | 64 | print(colored(f"\nDay with Highest Volume: {highest_volume_day}", 'cyan')) 65 | print(colored(f"Mean Closing Price: {mean_close}", 'red')) 66 | print(colored(f"Standard Deviation of Closing Prices: {std_dev_close}", 'yellow')) 67 | print(colored(f"Maximum Closing Price: {max_close} on {date_max_close}", 'magenta')) 68 | print(colored(f"Minimum Closing Price: {min_close} on {date_min_close}", 'magenta')) 69 | print(colored(f"Number of Trading Days: {trading_days_count}", 'cyan')) 70 | print(colored(f"Most Recent Trading Date: {last_trading_date}", 'cyan')) 71 | print(colored(f"Random Date: {random_date}, Random Closing Price: {random_price}", 'green')) 72 | 73 | --------------------------------------------------------------------------------