Last active
April 19, 2020 03:07
-
-
Save siddydutta/37715dc5803037da75687fb09ce93cb6 to your computer and use it in GitHub Desktop.
Scraping Coronavirus Data from Worldometers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import csv | |
url = 'https://www.worldometers.info/coronavirus/' | |
resp = requests.get(url) | |
soup = BeautifulSoup(resp.content, 'lxml') | |
data_table = soup.find('table', attrs={'id':'main_table_countries_today'}) | |
file = open('data.csv', 'w', newline='') | |
writer = csv.writer(file) | |
data = list(cell.text for cell in data_table.find('tr').find_all('th')) | |
writer.writerow(data) | |
for row in data_table.find_all('tr'): | |
if(row.has_attr('style')): | |
if (row.attrs['style'] == 'display: none'): | |
continue | |
data = list(cell.text.strip() for cell in row.find_all('td')) | |
writer.writerow(data) | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment