Advertisement
horozov86

Countries

Sep 14th, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4.  
  5. def fetch_data_rows(url):
  6.  
  7.     response = requests.get(url)
  8.  
  9.     if response.status_code == 200:
  10.  
  11.         soup = BeautifulSoup(response.text, 'html.parser')
  12.  
  13.         table = soup.find('table', {'class': 'wikitable'})
  14.  
  15.         if table:
  16.             rows = table.find_all('tr')
  17.  
  18.             data_rows = []
  19.             for row in rows:
  20.                 cells = row.find_all('td')
  21.                 data = [cell.get_text(strip=True) for cell in cells]
  22.                 if data:
  23.                     data_rows.append(data)
  24.  
  25.             return data_rows
  26.         else:
  27.             print("Table 'wikitable' was not found.")
  28.     else:
  29.         print("Failed to retrieve the page. Status code:", response.status_code)
  30.  
  31.     return []
  32.  
  33.  
  34. url = 'https://en.wikipedia.org/wiki/List_of_European_Union_member_states_by_population'
  35.  
  36. data_rows = fetch_data_rows(url)
  37.  
  38. countries_dictionary = {}
  39.  
  40. for row in data_rows:
  41.  
  42.     country = row[0]
  43.     population = row[1]
  44.     percent = row[2]
  45.  
  46.     percent = percent.strip('%')
  47.     percent = float(percent) if percent else None
  48.  
  49.     population = ''.join(filter(str.isdigit, population))
  50.     if population:
  51.         population = int(population)
  52.  
  53.         countries_dictionary[country] = {
  54.             'population': population,
  55.             'percent': percent,
  56.         }
  57.  
  58. print(countries_dictionary)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement