Webscraping - Reading trends from macrotrends with plotting

In the below steps, we will read crude oil historical data, and plot the trend

Preparing the enviroment:

In [1]:
import pandas as pd
import requests
from bs4 import BeautifulSoup

Now, we will form the url and then parse the text to html using BeautifulSoap

In [2]:
url="https://www.macrotrends.net/1369/crude-oil-price-history-chart"
crude_data  = requests.get(url).text
soup = BeautifulSoup(crude_data, 'html5lib')

Now we will read the table in the html and place it into pandas dataframe:

In [3]:
crude_data = pd.DataFrame(columns=["Year", "Average Closing Price", "Year Open", "Year High", "Year Low", "Year Close","Annual % Change"])
for row in soup.find("tbody").find_all('tr'):
    col = row.find_all("td")
    year = col[0].text
    acp = col[1].text
    yo = col[2].text
    yh = col[3].text
    yl = col[4].text
    yc = col[5].text
    apc = col[6].text

    crude_data= crude_data.append({"Year":year, "Average Closing Price":acp, "Year Open":yo, "Year High":yh, "Year Low":yl, "Year Close":yc, "Annual % Change":apc}, ignore_index=True)

Let's print the dataframe:

In [4]:
#we will print the first 5 rows (the past 5 years)
crude_data.head()
Out[4]:
Year Average Closing Price Year Open Year High Year Low Year Close Annual % Change
0 2022 $95.70 $76.08 $123.70 $76.08 $106.95 42.20%
1 2021 $68.17 $47.62 $84.65 $47.62 $75.21 55.01%
2 2020 $39.68 $61.17 $63.27 $11.26 $48.52 -20.64%
3 2019 $56.99 $46.31 $66.24 $46.31 $61.14 35.42%
4 2018 $65.23 $60.37 $77.41 $44.48 $45.15 -25.32%

Let's now simply plot the data

In [5]:
crude_data.plot(x ='Year', y='Average Closing Price', kind = 'scatter')
Out[5]:
<AxesSubplot:xlabel='Year', ylabel='Average Closing Price'>