Lab 2 - Web Scraping Backend Layer (refers to data access and manipulation) : Using BeautifulSoup, scrape this website: "https://en.wikipedia.org/wiki/List_of_countries_by_carbon_dioxide_emissions"...

1 answer below »
Lab 2 - Web Scraping


Backend Layer (refers to data access and manipulation) :


Using BeautifulSoup, scrape this website:


"https://en.wikipedia.org/wiki/List_of_countries_by_carbon_dioxide_emissions"


Scrape the "List of countries by carbon dioxide emissions" for the data. Store the scraped data in an object from your SQLite database class from lab1.


Frontend Layer (refers to the user interface):


Using the database passed from the backend, sort the data by the Fossil CO2 Emissions 2017 (% of the world) column. Extract the top 10 countries' data, and plot them in a pie-graph using your Graphics module from lab1.




I attach lab 1 as well


full-py-coccvake-x01t0ecf/full-py/backend.py # -*- coding: utf-8 -*- """ Created on Wed Oct 13 10:44:44 2021 @author: Sathish """ import sqlite3 global year,median,upper,lower year=[] median=[] upper=[] lower=[] conn = sqlite3.connect('tempe.db') print("Opened database successfully") cursor = conn.execute("SELECT YEAR,MEDIAN,UPPER,LOWER from TEMPDATA") for row in cursor: year.append(row[0]) median.append(row[1]) upper.append(row[2]) lower.append(row[3]) print("YEAR = ", row[0]) print("MEDIAN = ", row[1]) print("UPPER = ", row[2]) print("LOWER = ", row[3], "\n") print("Operation done successfully") conn.close() class __ret__(): def __init__(self): self.year=year self.median=median self.upper=upper self.lower=lower def getdata(self): return self.year,self.median,self.upper,self.lower full-py-coccvake-x01t0ecf/full-py/database.py # -*- coding: utf-8 -*- """ Created on Mon Oct 11 14:46:42 2021 @author: Sathish """ import sqlite3 conn = sqlite3.connect('tempe.db') print("Opened database successfully") conn.execute('''CREATE TABLE TEMPDATA (YEAR INT PRIMARY KEY NOT NULL, MEDIAN FLOAT NOT NULL, UPPER FLOAT NOT NULL, LOWER FLOAT);''') print("Table created successfully") conn.close() y=[] m=[] u=[] lo=[] l1=[] with open('temperature.txt') as f: lines=f.readlines() for i in range(1,len(lines)): l1=lines[i].split() y.append(int(l1[0])) m.append(float(l1[1])) u.append(float(l1[2])) lo.append(float(l1[3])) import sqlite3 print("Opened database successfully") for i in range(len(lines)-1): conn = sqlite3.connect('tempe.db') yy=y[i] mm=m[i] uu=u[i] loo=lo[i] conn.execute("INSERT INTO TEMPDATA (YEAR,MEDIAN,UPPER,LOWER) \ VALUES (?, ?, ?, ?)",(yy, mm, uu, loo)) conn.commit() conn.close() print("Records created successfully") full-py-coccvake-x01t0ecf/full-py/front.py from tkinter import * import numpy as np import matplotlib.pyplot as plt import backend from graph import graph from graph import graph1 from graph import graph2 global year,median,upper,lower # Created a class object object1 = backend.__ret__() year,median,upper,lower=object1.getdata() #Create an instance of Tkinter frame win= Tk() #Set the geometry of tkinter frame win.geometry("100x250") def g1(): graph(year,median,upper,lower) def g2(): graph1(year,median,upper,lower) def g3(): graph2(year,median,upper,lower) #Create a button to show the plot Button(win, text= "X-Y Plot", command= g1).pack(pady=20) Button(win, text= "Bar Graph", command= g2).pack(pady=20) Button(win, text= "Linear Regression", command= g3).pack(pady=20) win.mainloop() full-py-coccvake-x01t0ecf/full-py/graph.py # -*- coding: utf-8 -*- """ Created on Wed Oct 13 10:57:25 2021 @author: Sathish """ import numpy as np import matplotlib.pyplot as plt def graph(year,median,upper,lower): plt.figure() plt.plot(year, upper, label = "Upper") # plotting the line 2 points plt.plot(year, median, label = "Median") plt.plot(year, lower, label = "Lower") # naming the x axis plt.xlabel('Years') # naming the y axis plt.ylabel('Temperature in Celsius') # giving a title to my graph plt.title('Average Temperature Anamoly,Global!') # show a legend on the plot plt.legend() plt.show() def graph1(year,median,upper,lower): plt.figure() plt.bar(year, upper, color ='r') plt.legend('upper') plt.bar(year, median, color ='g') plt.legend('median') plt.bar(year, lower, color ='b') plt.legend('lower') # naming the x axis plt.xlabel('Years') # naming the y axis plt.ylabel('Temperature in Celsius') # giving a title to my graph plt.title('Average Temperature Anamoly,Global!') # show a legend on the plot plt.show() from scipy import stats def graph2(year,median,upper,lower): global slope,intercept x=year y=upper slope, intercept, r, p, std_err = stats.linregress(x, y) def myfunc(x): return slope * x + intercept mymodel = list(map(myfunc,x)) plt.figure() plt.scatter(x, y,color ='g') plt.plot(x, mymodel,color ='r') plt.legend('Upper') # naming the x axis plt.xlabel('Years') # naming the y axis plt.ylabel('Temperature in Celsius for Upper') # giving a title to my graph plt.title('Linear Regression Graph!') plt.show() global slope1,intercept1 x=year y=lower slope1, intercept1, r, p, std_err = stats.linregress(x, y) def myfunc1(x): return slope1 * x + intercept1 mymodel1 = list(map(myfunc1,x)) plt.figure() plt.scatter(x, y,color ='g') plt.plot(x, mymodel1,color ='r') plt.legend('Lower') # naming the x axis plt.xlabel('Years') # naming the y axis plt.ylabel('Temperature in Celsius for Lower') # giving a title to my graph plt.title('Linear Regression Graph!') plt.show() global slope2,intercept2 x=year y=median slope2, intercept2, r, p, std_err = stats.linregress(x, y) def myfunc2(x): return slope2 * x + intercept2 mymodel2 = list(map(myfunc2,x)) plt.figure() plt.scatter(x, y,color ='g') plt.plot(x, mymodel2,color ='r') plt.legend('Median') # naming the x axis plt.xlabel('Years') # naming the y axis plt.ylabel('Temperature in Celsius for MEdian') # giving a title to my graph plt.title('Linear Regression Graph!') plt.show() full-py-coccvake-x01t0ecf/full-py/Temperature.html # Average temperature # Relative to 1961-1990 # Celcius YearMedianUpperLower 1850-0.373-0.339-0.425 1851-0.218-0.184-0.274 1852-0.228-0.196-0.28 1853-0.269-0.239-0.321 1854-0.248-0.218-0.301 1855-0.272-0.241-0.324 1856-0.358-0.327-0.413 1857-0.461-0.431-0.512 1858-0.467-0.435-0.521 1859-0.284-0.249-0.34 1860-0.343-0.31-0.405 1861-0.407-0.356-0.484 1862-0.524-0.471-0.597 1863-0.278-0.226-0.359 1864-0.494-0.448-0.564 1865-0.279-0.232-0.358 1866-0.251-0.197-0.338 1867-0.321-0.265-0.406 1868-0.238-0.185-0.326 1869-0.262-0.221-0.333 1870-0.276-0.239-0.34 1871-0.335-0.293-0.404 1872-0.227-0.187-0.29 1873-0.304-0.258-0.364 1874-0.368-0.325-0.435 1875-0.395-0.352-0.464 1876-0.384-0.335-0.453 1877-0.075-0.026-0.146 18780.0350.084-0.026 1879-0.23-0.189-0.291 1880-0.227-0.182-0.288 1881-0.2-0.157-0.259 1882-0.213-0.171-0.274 1883-0.296-0.251-0.35 1884-0.409-0.373-0.457 1885-0.389-0.354-0.444 1886-0.367-0.334-0.419 1887-0.418-0.382-0.476 1888-0.307-0.268-0.359 1889-0.171-0.129-0.235 1890-0.416-0.369-0.484 1891-0.33-0.276-0.408 1892-0.455-0.4-0.526 1893-0.473-0.416-0.547 1894-0.41-0.352-0.487 1895-0.39-0.335-0.46 1896-0.186-0.136-0.255 1897-0.206-0.153-0.273 1898-0.412-0.362-0.479 1899-0.289-0.231-0.355 1900-0.203-0.147-0.272 1901-0.259-0.208-0.326 1902-0.402-0.355-0.461 1903-0.479-0.431-0.543 1904-0.52-0.471-0.58 1905-0.377-0.326-0.438 1906-0.283-0.232-0.338 1907-0.465-0.423-0.523 1908-0.511-0.468-0.563 1909-0.522-0.476-0.576 1910-0.49-0.448-0.547 1911-0.544-0.498-0.595 1912-0.437-0.392-0.486 1913-0.424-0.381-0.471 1914-0.244-0.197-0.291 1915-0.141-0.085-0.202 1916-0.383-0.328-0.437 1917-0.468-0.413-0.525 1918-0.333-0.27-0.396 1919-0.275-0.217-0.327 1920-0.247-0.198-0.296 1921-0.187-0.149-0.237 1922-0.302-0.262-0.346 1923-0.276-0.237-0.323 1924-0.294-0.259-0.34 1925-0.215-0.178-0.265 1926-0.108-0.07-0.155 1927-0.21-0.177-0.253 1928-0.206-0.174-0.253 1929-0.35-0.32-0.394 1930-0.137-0.104-0.181 1931-0.087-0.055-0.133 1932-0.137-0.107-0.183 1933-0.273-0.24-0.32 1934-0.131-0.1-0.177 1935-0.178-0.15-0.221 1936-0.147-0.12-0.186 1937-0.0260.001-0.064 1938-0.0060.017-0.044 1939-0.052-0.029-0.088 19400.0140.045-0.018 19410.020.053-0.021 1942-0.0270.015-0.081 1943-0.0040.036-0.056 19440.1440.180.094 19450.0250.059-0.017 1946-0.071-0.042-0.113 1947-0.0380.001-0.098 1948-0.0390-0.101 1949-0.074-0.036-0.119 1950-0.173-0.135-0.216 1951-0.052-0.018-0.091 19520.0280.063-0.01 19530.0970.1320.06 1954-0.129-0.093-0.166 1955-0.19-0.16-0.222 1956-0.267-0.236-0.297 1957-0.0070.024-0.035 19580.0460.0750.02 19590.0170.043-0.008 1960-0.049-0.023-0.073 19610.0380.060.009 19620.0140.034-0.012 19630.0480.0740.022 1964-0.223-0.197-0.246 1965-0.14-0.113-0.163 1966-0.068-0.042-0.09 1967-0.074-0.052-0.093 1968-0.113-0.097-0.133 19690.0320.050.014 1970-0.027-0.009-0.048 1971-0.186-0.163-0.204 1972-0.065-0.04-0.08 19730.0620.0820.051 1974-0.214-0.197-0.227 1975-0.149-0.134-0.163 1976-0.241-0.227-0.255 19770.0470.0640.034 1978-0.062-0.044-0.077 19790.0570.0760.041 19800.0920.1070.077 19810.140.1530.125 19820.0110.03-0.011 19830.1940.2110.174 1984-0.0140.006-0.033 1985-0.03-0.013-0.048 19860.0450.0670.024 19870.1920.220.171 19880.1980.2210.175 19890.1180.1470.091 19900.2960.330.267 19910.2540.2890.22 19920.1050.1390.071 19930.1480.1860.112 19940.2080.2470.174 19950.3250.3610.289 19960.1830.2170.146 19970.390.4250.356 19980.5390.5780.5 19990.3060.3430.265 20000
Answered 7 days AfterOct 19, 2021

Answer To: Lab 2 - Web Scraping Backend Layer (refers to data access and manipulation) : Using BeautifulSoup,...

Vicky answered on Oct 26 2021
119 Votes
# import libraries
from bs4 import BeautifulSoup
import pandas as pd
import requests
# Extract h
tml page content from url
url = "https://en.wikipedia.org/wiki/List_of_countries_by_carbon_dioxide_emissions"
content = requests.get(url)
soup = BeautifulSoup(content.text, "html.parser")
# Create list of all the columns of table
Countries=[]
one990 = []
two005 = []
two017 = []
two017world= []
two017vs1990 = []
perlandarea = []
percapita = []
totalincluding= []
totalexcluding = []
# Find all the elements of table
job = soup.find('table',attrs={'class':'wikitable sortable','style':'text-align:right'})
job_elems = job.find_all("td")
# Add table data to lists
for i in range(len(job_elems)):
if(i%10==0):
Countries.append(job_elems[i].text[1:])
elif(i%10==1):
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here