-
Notifications
You must be signed in to change notification settings - Fork 1
/
covidproject webs.txt
52 lines (44 loc) · 1.35 KB
/
covidproject webs.txt
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
url = "https://www.ndtv.com/coronavirus/india-covid-19-tracker"
import requests #import requests library
page = requests.get(url)
page
page.text
from bs4 import BeautifulSoup
dir(BeautifulSoup)
soup = BeautifulSoup(page.text)
soup
soup.title
soup.table
soup = BeautifulSoup(page.text,'html.parser')
soup.table
table = soup.find_all('table')
table
rows = table[0].find_all('tr')
rows
[i.text for i in rows[0]] # To get Headings
[i.text.replace('\n','') for i in rows[0]] #Replace \n with empty string
[i.text.replace('\n','').strip() for i in rows[0]] #Remove empty spaces
headings = [i.text.replace('\n','').strip() for i in rows[0]] # Assigning a variable
rows = table[0].find_all('tr')
rows
for i in rows:
print(i)
all_rows = []
for i in rows:
td = i.find_all('td')
row = [i.text for i in td]
all_rows.append(row) #get the rows in a list
all_rows
all_rows.pop(0) #remove first element
all_rows
for i in all_rows:
i[0] = i[0][:i[0].find("DistrictCases")] #Remove the part of first element after 'DistrictCases'
for i in all_rows:
i[1] = i[1][:i[1].find(" ")]
i[2] = i[2][:i[2].find(" ")]
i[3] = i[3][:i[3].find(" ")]
i[4] = i[4][:i[4].find(" ")] #remove inc/dec part after space
all_rows #data after cleansing
import pandas as pd
df = pd.DataFrame(all_rows,columns=headings)
df