Code: Select all
import pandas as pd
import requests
from bs4 import BeautifulSoup
html = requests.get('https://en.wikipedia.org/wiki/List_of_countries_by_Human_Development_Index')
soup = BeautifulSoup(html.text, 'html.parser')
table = soup.find('table', {'class': 'wikitable'})
rows = table.find_all('tr')[1:]
countries = []
hdi_index = []
for row in rows:
cols = row.find_all('td')
if len(cols) > 2:
countries.append(cols[1].text.strip())
hdi_index.append(cols[2].text.strip())
hdi_df = pd.DataFrame({'Country': countries, 'HDI': hdi_index})
print(hdi_df.head())