-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
101 lines (71 loc) · 3.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import requests
from bs4 import BeautifulSoup
import csv
def fetch_page_content(url):
"""Fetch the HTML content of a given URL."""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
}
try:
page = requests.get(url, headers=headers, timeout=5)
page.raise_for_status()
return page.content # Return HTML content if no error occurs
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.Timeout as timeout_err:
print(f"Timeout error occurred: {timeout_err}")
except requests.exceptions.ConnectionError as connect_err:
print(f"Connection error occurred. {connect_err}")
except requests.exceptions.RequestException as error:
print(f"An error occurred: {error}")
return None
def parse_page_content(html_content):
"""Parse HTML content and extract population data."""
if not html_content:
print("No HTML content to parse.")
return None
soup = BeautifulSoup(html_content, 'lxml')
# Find the table container
table = soup.find('table', id='example2').tbody
# Find all rows withing the table
rows = table.find_all('tr')
countries_list = [] # empty list
# Loop through each row to get the corresponding column value
for row in rows:
dic = {} # empty dict
columns = row.find_all('td')
dic['#'] = columns[0].text
dic['Country (or dependency)'] = columns[1].text
dic['Population (2024)'] = columns[2].text.replace(',', '')
dic['Yearly Change'] = columns[3].text
dic['Net Change'] = columns[4].text
dic['Density (P/Km²)'] = columns[5].text.replace(',', '')
dic['Land Area (Km²)'] = columns[6].text.replace(',', '')
dic['Migrants (net)'] = columns[7].text.replace(',', '')
dic['Fert. Rate'] = columns[8].text
dic['Med. Age'] = columns[9].text
dic['Urban Pop %'] = columns[10].text
dic['World Share'] = columns[11].text
countries_list.append(dic) # Append dictionary to the list
return countries_list # Return the list of dictionaries
def save_to_csv(data):
"""Save the extracted data to a CSV file."""
with open('data.csv', 'w', encoding='utf-8') as f:
# Define the desired fieldnames for the CSV file
fieldnames = ['#', 'Country (or dependency)', 'Population (2024)', 'Yearly Change', 'Net Change',
'Density (P/Km²)', 'Land Area (Km²)', 'Migrants (net)', 'Fert. Rate', 'Med. Age', 'Urban Pop %',
'World Share']
# Create the CSV DictWriter object
csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
# Write the header row
csv_writer.writeheader()
# Write each row to the CSV file
csv_writer.writerows(data)
def population_table_scraper():
"""Main function to scrape population data and save it to a CSV file."""
page_url = "https://www.worldometers.info/world-population/population-by-country/"
html = fetch_page_content(page_url)
population_data = parse_page_content(html)
save_to_csv(population_data)
if __name__ == '__main__':
population_table_scraper()