!pip install yfinance requests beautifulsoup4 matplotlib Collecting yfinance Downloading yfinance-0.2.66-py2.py3-none-any.whl.metadata (6.0 kB) Requirement already satisfied: requests in /opt/conda/lib/python3.12/site-packages (2.32.3) Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.12/site-packages (4.12.3) Collecting matplotlib Downloading matplotlib-3.10.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (11 kB) Collecting pandas>=1.3.0 (from yfinance) Downloading pandas-2.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (91 kB) Collecting numpy>=1.16.5 (from yfinance) Downloading numpy-2.3.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (62 kB) Collecting multitasking>=0.0.7 (from yfinance) Downloading multitasking-0.0.12.tar.gz (19 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: platformdirs>=2.0.0 in /opt/conda/lib/python3.12/site-packages (from yfinance) (4.3.6) Requirement already satisfied: pytz>=2022.5 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2024.2) Requirement already satisfied: frozendict>=2.3.4 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.4.6) Collecting peewee>=3.16.2 (from yfinance) Downloading peewee-3.18.2.tar.gz (949 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 949.2/949.2 kB 46.8 MB/s eta 0:00:00 Installing build dependencies ... one Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Collecting curl_cffi>=0.7 (from yfinance) Downloading curl_cffi-0.13.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (13 kB) Collecting protobuf>=3.19.0 (from yfinance) Downloading protobuf-6.32.1-cp39-abi3-manylinux2014_x86_64.whl.metadata (593 bytes) Collecting websockets>=13.0 (from yfinance) Downloading websockets-15.0.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.8 kB) Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.12/site-packages (from requests) (3.4.1) Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.12/site-packages (from requests) (3.10) Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.12/site-packages (from requests) (2.3.0) Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.12/site-packages (from requests) (2024.12.14) Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.12/site-packages (from beautifulsoup4) (2.5) Collecting contourpy>=1.0.1 (from matplotlib) Downloading contourpy-1.3.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (5.5 kB) Collecting cycler>=0.10 (from matplotlib) Downloading cycler-0.12.1-py3-none-any.whl.metadata (3.8 kB) Collecting fonttools>=4.22.0 (from matplotlib) Downloading fonttools-4.60.0-cp312-cp312-manylinux1_x86_64.manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_5_x86_64.whl.metadata (111 kB) Collecting kiwisolver>=1.3.1 (from matplotlib) Downloading kiwisolver-1.4.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (6.3 kB) Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.12/site-packages (from matplotlib) (24.2) Collecting pillow>=8 (from matplotlib) Downloading pillow-11.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (9.0 kB) Collecting pyparsing>=2.3.1 (from matplotlib) Downloading pyparsing-3.2.5-py3-none-any.whl.metadata (5.0 kB) Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/lib/python3.12/site-packages (from matplotlib) (2.9.0.post0) Requirement already satisfied: cffi>=1.12.0 in /opt/conda/lib/python3.12/site-packages (from curl_cffi>=0.7->yfinance) (1.17.1) Collecting tzdata>=2022.7 (from pandas>=1.3.0->yfinance) Downloading tzdata-2025.2-py2.py3-none-any.whl.metadata (1.4 kB) Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.12/site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0) Requirement already satisfied: pycparser in /opt/conda/lib/python3.12/site-packages (from cffi>=1.12.0->curl_cffi>=0.7->yfinance) (2.22) Downloading yfinance-0.2.66-py2.py3-none-any.whl (123 kB) Downloading matplotlib-3.10.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (8.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.7/8.7 MB 137.5 MB/s eta 0:00:00 Downloading contourpy-1.3.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (362 kB) Downloading curl_cffi-0.13.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.3/8.3 MB 118.4 MB/s eta 0:00:00 Downloading cycler-0.12.1-py3-none-any.whl (8.3 kB) Downloading fonttools-4.60.0-cp312-cp312-manylinux1_x86_64.manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_5_x86_64.whl (4.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.9/4.9 MB 113.8 MB/s eta 0:00:00 Downloading kiwisolver-1.4.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (1.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 71.1 MB/s eta 0:00:00 Downloading numpy-2.3.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (16.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.6/16.6 MB 171.9 MB/s eta 0:00:00 Downloading pandas-2.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.0/12.0 MB 157.5 MB/s eta 0:00:00 Downloading pillow-11.3.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.6/6.6 MB 147.0 MB/s eta 0:00:00 Downloading protobuf-6.32.1-cp39-abi3-manylinux2014_x86_64.whl (322 kB) Downloading pyparsing-3.2.5-py3-none-any.whl (113 kB) Downloading websockets-15.0.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182 kB) Downloading tzdata-2025.2-py2.py3-none-any.whl (347 kB) Building wheels for collected packages: multitasking, peewee Building wheel for multitasking (setup.py) ... one Created wheel for multitasking: filename=multitasking-0.0.12-py3-none-any.whl size=15605 sha256=ab6080b169b4d43036c62b4c556476319994dad0f5aba2bbb6443b4ed5af9dca Stored in directory: /home/jupyterlab/.cache/pip/wheels/cc/bd/6f/664d62c99327abeef7d86489e6631cbf45b56fbf7ef1d6ef00 Building wheel for peewee (pyproject.toml) ... one Created wheel for peewee: filename=peewee-3.18.2-cp312-cp312-linux_x86_64.whl size=303862 sha256=34d6504c0a8afa534ec4edc4a7b6863a607bc2e165a5d3ebdb7b7fcfc53bd6f6 Stored in directory: /home/jupyterlab/.cache/pip/wheels/d1/df/a9/0202b051c65b11c992dd6db9f2babdd2c44ec7d35d511be5d3 Successfully built multitasking peewee Installing collected packages: peewee, multitasking, websockets, tzdata, pyparsing, protobuf, pillow, numpy, kiwisolver, fonttools, cycler, pandas, curl_cffi, contourpy, yfinance, matplotlib Successfully installed contourpy-1.3.3 curl_cffi-0.13.0 cycler-0.12.1 fonttools-4.60.0 kiwisolver-1.4.9 matplotlib-3.10.6 multitasking-0.0.12 numpy-2.3.3 pandas-2.3.2 peewee-3.18.2 pillow-11.3.0 protobuf-6.32.1 pyparsing-3.2.5 tzdata-2025.2 websockets-15.0.1 yfinance-0.2.66 Q1. Extracting Tesla Stock Data Using yfinance import yfinance as yf import pandas as pd
tesla = yf.Ticker("TSLA") tesla_data = tesla.history(period="max") tesla_data.reset_index(inplace=True) tesla_data.head() Date Open High Low Close Volume Dividends Stock Splits 0 2010-06-29 00:00:00-04:00 1.266667 1.666667 1.169333 1.592667 281494500 0.0 0.0 1 2010-06-30 00:00:00-04:00 1.719333 2.028000 1.553333 1.588667 257806500 0.0 0.0 2 2010-07-01 00:00:00-04:00 1.666667 1.728000 1.351333 1.464000 123282000 0.0 0.0 3 2010-07-02 00:00:00-04:00 1.533333 1.540000 1.247333 1.280000 77097000 0.0 0.0 4 2010-07-06 00:00:00-04:00 1.333333 1.333333 1.055333 1.074000 103003500 0.0 0.0 Q2. Use Webscraping to Extract Tesla Revenue Data import requests import pandas as pd from bs4 import BeautifulSoup
url_tesla = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue" html_data = requests.get(url_tesla).text soup = BeautifulSoup(html_data, "html.parser") tesla_revenue = pd.DataFrame(columns=["Date", "Revenue"]) tables = soup.find_all("table") for table in tables: rows = table.find_all("tr") for row in rows: cols = row.find_all("td") if len(cols) == 2: date = cols[0].text.strip() revenue = cols[1].text.strip() tesla_revenue = pd.concat( [tesla_revenue, pd.DataFrame([[date, revenue]], columns=["Date", "Revenue"])], ignore_index=True ) tesla_revenue = tesla_revenue[tesla_revenue["Revenue"] != ""].dropna() tesla_revenue.tail() Date Revenue Q3. Use yfinance to Extract GameStop Stock Data gme = yf.Ticker("GME") gme_data = gme.history(period="max") gme_data.reset_index(inplace=True) gme_data.head() Date Open High Low Close Volume Dividends Stock Splits 0 2002-02-13 00:00:00-05:00 1.620128 1.693350 1.603296 1.691666 76216000 0.0 0.0 1 2002-02-14 00:00:00-05:00 1.712707 1.716074 1.670626 1.683250 11021600 0.0 0.0 2 2002-02-15 00:00:00-05:00 1.683251 1.687459 1.658002 1.674834 8389600 0.0 0.0 3 2002-02-19 00:00:00-05:00 1.666418 1.666418 1.578047 1.607504 7410400 0.0 0.0 4 2002-02-20 00:00:00-05:00 1.615921 1.662210 1.603296 1.662210 6892800 0.0 0.0 Q4. Use Webscraping to Extract GameStop Revenue Data url_gme = "https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue" html_data = requests.get(url_gme).text soup = BeautifulSoup(html_data, "html.parser")
gme_revenue = pd.DataFrame(columns=["Date", "Revenue"]) for row in soup.find_all("tr"): cols = row.find_all("td") if len(cols) == 2: date = cols[0].text.strip() revenue = cols[1].text.strip() gme_revenue = pd.concat([gme_revenue, pd.DataFrame([[date, revenue]], columns=["Date", "Revenue"])], ignore_index=True)
gme_revenue = gme_revenue[gme_revenue["Revenue"] != ""].dropna() gme_revenue.tail() Date Revenue Q5. Plot Tesla Stock Graph import matplotlib.pyplot as plt
def make_graph(stock_data, revenue_data, stock_name): fig, ax1 = plt.subplots(figsize=(10,4)) ax1.plot(stock_data["Date"], stock_data["Close"], color="tab:blue") ax1.set_ylabel("Stock Price") ax1.set_title(f"{stock_name} Stock Data") plt.show()
make_graph(tesla_data, tesla_revenue, "Tesla")
Q6. Plot GameStop Stock Graph make_graph(gme_data, gme_revenue, "GameStop")