<jupyter_start><jupyter_text>Web Scraping Stock Data of Tesla and plotting a chart of its Annual Revenue<jupyter_code># importing the necessary libraries
import pandas as pd
import requests
from bs4 import BeautifulSoup
# www.macrotrends.net is a popular stock market website. I will use it to get Tesla's information
dividends = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0220ENSkillsNetwork23455606-2021-01-01"
data = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/stock-price-history"
#getting request data
tesla_dividends = requests.get(dividends).text
tesla_data = requests.get(data).text
#Creating Beautiful Soup Objects
tesla_div_soup = BeautifulSoup(tesla_dividends, "html.parser")
tesla_data_soup = BeautifulSoup(tesla_data, "html.parser")
#Creating a dataframe to store information about historical data of the stock
tesla_history = pd.DataFrame(columns=["Year", "Stock Price", "Open", "High", "Low", "Close", "Annual %Change"])

# First we isolate the body of the table which contains all the information
# Then we loop through each row and find all the column values for each row
for row in tesla_data_soup.find("tbody").find_all('tr'):
    col = row.find_all("td")
    year = col[0].text
    price = col[1].text
    Open = col[2].text
    high = col[3].text
    low = col[4].text
    close = col[5].text
    change = col[6].text
    
    # Finally we append the data of each row to the table
    tesla_history = tesla_history.append({"Year":year, "Stock Price": price, "Open":Open,
                                      "High":high, "Low":low, "Close":close,
                                      "Annual %Change":change}, ignore_index=True)    
tesla_history
tesla_div_soup.title
tesla = pd.DataFrame(columns = ["Date", "Revenue(Millions of US $)"])

for row in tesla_div_soup.tbody.find_all('tr'):
    col = row.find_all('td')
    Year = col[0].text
    Rev = col[1].text
    tesla = tesla.append({"Date":Year, "Revenue(Millions of US $)":Rev}, ignore_index=True)
    
tesla
# Stripping the Revenue columns off the special Characters
tesla['Revenue(Millions of US $)'] = tesla['Revenue(Millions of US $)'].replace('[$,]',"", regex = True)
tesla<jupyter_output><empty_output><jupyter_text>Checking the data type of the columns<jupyter_code>tesla.dtypes
tesla = tesla.astype('int32')
tesla.dtypes<jupyter_output><empty_output><jupyter_text>Plotting a line chart of Revenue per Year<jupyter_code>%matplotlib inline
import matplotlib as plt
from matplotlib import pyplot 

plt.pyplot.plot(tesla['Date'], tesla['Revenue(Millions of US $)'])
plt.pyplot.xlabel("Year")
plt.pyplot.ylabel("Revenue in Millions of US $")
plt.pyplot.title("Revenue per Year")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11927877947295423, 'fraction_numerical': 0.0159500693481276, 'mean_word_length': 4.887755102040816, 'pattern_counts': {'":': 9, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '564041', 'license': 'MIT', 'n_tokens_mistral': 1037, 'n_tokens_neox': 968, 'n_words': 239, 'path': 'Stock Data.ipynb', 'repo_name': 'RickyDonne/Web-Scraping', 'size': '2884'}