Coronavirus, one of the biggest pandemic has brought all the world to Danger. Along with this, it is one of the trending News, everyone has this day. In this article, we will be scraping data and printing Covid-19 statistics in human-readable form. The data will be scraped from this website
Prerequisites:
- The libraries ‘requests’, ‘bs4’, and ‘texttable’ have to be installed –
pip install bs4 pip install requests pip install texttable
Project : Let’s head over to code, create a file called run.py.
Python3
# importing modulesimport requestsfrom bs4 import BeautifulSoup# URL for scrapping data# get URL htmlpage = requests.get(url)soup = BeautifulSoup(page.text, 'html.parser')data = []# soup.find_all('td') will scrape every# element in the url's tabledata_iterator = iter(soup.find_all('td')) # data_iterator is the iterator of the table# This loop will keep repeating till there is# data available in the iteratorwhile True: try: country = next(data_iterator).text confirmed = next(data_iterator).text deaths = next(data_iterator).text continent = next(data_iterator).text # For 'confirmed' and 'deaths', # make sure to remove the commas # and convert to int data.append(( country, int(confirmed.replace(', ', '')), int(deaths.replace(', ', '')), continent )) # StopIteration error is raised when # there are no more elements left to # iterate through except StopIteration: break# Sort the data by the number of confirmed casesdata.sort(key = lambda row: row[1], reverse = True) |
To print the data in human-readable format, we will use the library ‘texttable‘
Python3
# create texttable objectimport texttable as tttable = tt.Texttable()# Add an empty row at the beginning for the headerstable.add_rows([(None, None, None, None)] + data) # 'l' denotes left, 'c' denotes center,# and 'r' denotes righttable.set_cols_align(('c', 'c', 'c', 'c')) table.header((' Country ', ' Number of cases ', ' Deaths ', ' Continent '))print(table.draw()) |
Output:
+---------------------------+-------------------+----------+-------------------+ | Country | Number of cases | Deaths | Continent | +===========================+===================+==========+===================+ | United States | 644348 | 28554 | North America | +---------------------------+-------------------+----------+-------------------+ | Spain | 180659 | 18812 | Europe | +---------------------------+-------------------+----------+-------------------+ | Italy | 165155 | 21645 | Europe | +---------------------------+-------------------+----------+-------------------+ ...
NOTE: The output depends on the current statistics
Stay home, stay safe!
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Implementing Web Scraping in Python with BeautifulSoup
- BeautifulSoup object - Python Beautifulsoup
- Scraping COVID-19 statistics using Python and Selenium
- Build a COVID19 Vaccine Tracker Using Python
- How to create a COVID19 Data Representation GUI?
- Scraping And Finding Ordered Words In A Dictionary using Python
- Implementing web scraping using lxml in Python
- Newspaper scraping using Python and News API
- Web Scraping CryptoCurrency price and storing it in MongoDB using Python
- Scraping Javascript Enabled Websites using Scrapy-Selenium
- Web Scraping using lxml and XPath in Python
- Data Scraping for Android Apps using google-play-scraper in Node.js
- Scraping Television Rating Point using Python
- Scraping Amazon Product Information using Beautiful Soup
- Web scraping from Wikipedia using Python - A Complete Guide
- Python - Obtain title, views and likes of YouTube video using BeautifulSoup
- Web Scrapping using Beautifulsoup and scrapingdog API
- Get tag name using Beautifulsoup in Python
- How to Remove tags using BeautifulSoup in Python?
- How to scrape all the text from body tag using Beautifulsoup in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manroopparmar120

