Covid-19 Statistics in 30 lines of python.
How about crawling the Ministry of Health Welfare for COVID-19 case statistics in just 5 minutes? You just need to have beautiful-soup installed.
- pip install bs4
- pip install requests
- pip install PTable
The basic idea of web scraping is to look for existing HTML data, using a web scraper to identify the data. Here we are looking for the HTML table-specific tags such as tr and td.
import sys
import requests
import re
from bs4 import BeautifulSoup
from prettytable import PrettyTable
def get_content(url):
response = requests.get(url, timeout=10)
content = BeautifulSoup(response.content, "html.parser")
return content
def get_stats(content):
soup_table = content.find('table')
table = PrettyTable()
table.field_names = ["State", "Confirmed", "Recovered", "Deaths"]
for row in soup_table.findAll('tr')[1:31]:
col = row.findAll('td')
state = col[1].getText()
confirmed = col[2].getText()
recovered = col[3].getText()
deaths = col[4].getText()
stats = [state, confirmed, recovered, deaths]
table.add_row(stats)
return table
if __name__ == '__main__':
content = get_content(url="https://www.mohfw.gov.in/")
table = get_stats(content)
print(table)
Thank You.