Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2017 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/cc15f86ba5f77b54e868e36316f41072 to your computer and use it in GitHub Desktop.
Save anonymous/cc15f86ba5f77b54e868e36316f41072 to your computer and use it in GitHub Desktop.
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