-
-
Save agawronski/4711e32003a153c83bb1d0bdb25fa50a to your computer and use it in GitHub Desktop.
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 csv | |
import requests | |
from BeautifulSoup import BeautifulSoup | |
url = 'http://www.worldometers.info/world-population/population-by-country/' | |
response = requests.get(url) #Getting the response from mentioned URL using get() method of requests | |
html = response.content | |
soup = BeautifulSoup(html) | |
table = soup.find('table', attrs={'id': 'example2'}) #From BeautifulSoup of HTML content, finding the tbody(data of table) of the desired table having specific attributes, here desired table has 'example2' as idtbody = table.find('tbody') | |
list_of_rows = [] | |
for row in tbody.findAll('tr')[0:]: #line 11-16, Traversing every row('tr') and every cell of a row ('td') in table and making list of rows list_of_cells = [] | |
for cell in row.findAll('td'): | |
text = cell.text.replace(' ', '') | |
list_of_cells.append(text) | |
list_of_rows.append(list_of_cells) | |
outputfile = open("./test.csv", "wb") | |
writer = csv.writer(outputfile) #Creating writer of output file using writer method of csv | |
writer.writerow(["Rank", "Country", "Population(2017)", "Yearly Change", "Net Change", "Density(P/Km^2)", "Land Area(Km^2)", "Migrants", "Fert. Rate", "Med Age", "Urban Population(%)", "World Share"]) | |
writer.writerows(list_of_rows) #Using the writer, writing the list of rows in output file i.e. test.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment