Skip to content

Instantly share code, notes, and snippets.

@agawronski
Forked from anonymous/webScraping.py
Created January 9, 2018 15:38
Show Gist options
  • Save agawronski/4711e32003a153c83bb1d0bdb25fa50a to your computer and use it in GitHub Desktop.
Save agawronski/4711e32003a153c83bb1d0bdb25fa50a 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