aletisunil / Covid19_liveTracker
Scraps data from website, regarding Covid19 status
Requirements for scraping a site
- Requests - Used to send all kinds of HTTP requests
- BeautifulSoup - Used for pulling data out of HTML and XML files
- lxml - Which allows easy handling of HTML and XML files
In this article, we will see how we scraped worldometers.info
1) First, you need to install all required modules for scraping a site
2) Open your favourite text editor, In my case I used VSCODE
3) Now let's code, First import requests, and bs4 modules by using import keyword
import requests import bs4 country_name=input("Enter the Country name: ") def covid19(country): res = requests.get("https://www.worldometers.info/coronavirus/#countries") soup = bs4.BeautifulSoup(res.text, 'lxml') index = -1 data=soup.select('tr td') for i in range(len(data)): if data[i].text.lower()==country.lower(): index=i break for i in range(7): if i == 0: print("\nCountry name: "+str(data[i+index].text)) elif i == 1: print("Total cases: "+str(data[i+index].text)) elif i == 2: if data[i+index].text == '': print("New cases: 0") else: print("New cases: "+str(data[i+index].text)) elif i == 3: print("Total deaths: "+str(data[i+index].text)) elif i == 4: if data[i+index].text == '': print("New deaths: 0") else: print("New deaths: "+str(data[i+index].text)) elif i == 5: print("Total Recovered: "+str(data[i+index].text)) elif i == 6: print("Active cases: "+str(data[i+index].text),end='\n\n') covid19(country_name)
4) Now save this code, in my case i saved it with "Covid19_liveTracker.py"
5) Now you can execute this python script by typing
python Covid19_liveTracker.py
let me know, if you face any errors or problems, Happy Quarantine
Stay Home, Stay Safe.
Hope this problem gets resolves very soon :)
Top comments (6)
Sorry, since it didn't work out of the box for me - I was running Python 2.7.17 - I had to play around with it for a while to remove the syntax errors...and...well, one thing led to another :) Still, I learned quite a bit here including how to post code. Never done anything in python before so here goes (without my comments to keep the code clean):
Thank you,
I would add a small elif to manage the case of a wrong country name input.
put the country_name=input("Enter the Country name: ") inside the function
and add an else statement :
else:
print("This country doesn't exist")
covid19()
Thank you for this article, looking forward to read more from you
Thanks for pointing it out :)
Good one. Looking forward to see more from you :)
Thanks dude 😊
I build it too: mzaini30.js.org/covid19/