-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebscrape.py
88 lines (52 loc) · 1.64 KB
/
webscrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import matplotlib.pyplot as plt
import requests
import bs4
import pandas as pd
import os
# Make requests from webpage
url = 'https://www.worldometers.info/coronavirus/country/canada/'
result = requests.get(url)
# Creating soap object
soup = bs4.BeautifulSoup(result.text,'lxml')
# Searching div tags having maincounter-number class
cases = soup.find_all('div' ,class_= 'maincounter-number')
class Corona:
data =[]
cases = '10'
def __init__(self):
for i in cases:
span = i.find('span')
self.data.append(span.string)
# print(self.data)
def __repr__(self):
return(self.data)
print(repr(Corona))
newCases = Corona()
# Creating dataframe
df = pd.DataFrame({"Corona Virus Infection Cases in Canada": newCases.data})
df.head(0).style.format({"Corona Cases in Canada": "{:20,.0f}"})
# Naming the columns
df.index = ['TotalCases', ' Death', 'Recovered']
# Exporting data into Excel
df.to_csv('Corona_Data.csv')
#Opening Excel File
File = os.path.abspath("Corona_Data.csv")
os.startfile(File)
var = pd.read_csv(os.path.abspath("Corona_Data.csv"))
df = pd.DataFrame(var)
X = list(df.iloc[:,0])
Y = list(df.iloc[:,1])
#Line Graph Representation of Statistics
plt.plot(X, Y, color='red', linestyle='dashed')
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()
# setting x and y axis range
# naming the x axis
plt.grid(color = 'green', alpha = 0.3, linestyle = '-', linewidth = 2)
plt.xlabel('Situation')
# naming the y axis
plt.ylabel('figures')
# giving a title to my graph
plt.title('Most Recent Canada Covid 19 Records')
# function to show the plot
plt.show()