Created
June 23, 2020 05:22
-
-
Save ShubhamPradhan007/8ba430395417432685ccd618a3f8eab6 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
# -*- 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