Skip to content

Instantly share code, notes, and snippets.

@ShubhamPradhan007
Created June 23, 2020 05:22
Show Gist options
  • Save ShubhamPradhan007/8ba430395417432685ccd618a3f8eab6 to your computer and use it in GitHub Desktop.
Save ShubhamPradhan007/8ba430395417432685ccd618a3f8eab6 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 08:49:30 2020
@author: admin
"""
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://www.worldometers.info/world-population/population-by-country/'
requests.get(url)
page = requests.get(url)
soup = BeautifulSoup(page.text, 'lxml')
print(soup)
table_data = soup.find('table', class_ = 'table table-striped table-bordered')
headers = []
for i in table_data.find_all('th'):
title = i.text
headers.append(title)
data = pd.DataFrame(columns = headers)
print(data)
for j in table_data.find_all('tr')[1:]:
row_data = j.find_all('td')
row = [tr.text for tr in row_data]
length = len(data)
data.loc[length] = row
print (data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment