CCCPaste

Countries

import requests
from bs4 import BeautifulSoup


def fetch_data_rows(url):

    response = requests.get(url)

    if response.status_code == 200:

        soup = BeautifulSoup(response.text, 'html.parser')

        table = soup.find('table', {'class': 'wikitable'})

        if table:
            rows = table.find_all('tr')

            data_rows = []
            for row in rows:
                cells = row.find_all('td')
                data = [cell.get_text(strip=True) for cell in cells]
                if data:
                    data_rows.append(data)

            return data_rows
        else:
            print("Table 'wikitable' was not found.")
    else:
        print("Failed to retrieve the page. Status code:", response.status_code)

    return []


url = 'https://en.wikipedia.org/wiki/List_of_European_Union_member_states_by_population'

data_rows = fetch_data_rows(url)

countries_dictionary = {}

for row in data_rows:

    country = row[0]
    population = row[1]
    percent = row[2]

    percent = percent.strip('%')
    percent = float(percent) if percent else None

    population = ''.join(filter(str.isdigit, population))
    if population:
        population = int(population)

        countries_dictionary[country] = {
            'population': population,
            'percent': percent,
        }

print(countries_dictionary)