-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountry_data.py
More file actions
62 lines (43 loc) · 1.19 KB
/
country_data.py
File metadata and controls
62 lines (43 loc) · 1.19 KB
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
import requests
from bs4 import BeautifulSoup
import csv
url = "https://www.scrapethissite.com/pages/simple/"
print(f"Scraping started : {url}")
html = requests.get(url).text
soup = BeautifulSoup(html, 'html.parser')
print("Page scraped successfully.")
print("Processing data...")
countries_list = [
country.text.strip()
for country in soup.find_all('h3')
]
data_1 = [data.text.strip() for data in soup.find_all('strong')]
data_2 = [data.text.strip() for data in soup.find_all('span')]
all_data = list(zip(data_1, data_2))
result = [
all_data[i:i+3]
for i in range(0, len(all_data), 3)
]
rows = []
for country, details in zip(countries_list, result):
row = {
"Country": country,
"Capital": details[0][1],
"Population": details[1][1],
"Area(km²)": details[2][1]
}
rows.append(row)
print("Data processed. Writing to CSV...")
with open('country_data.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(
f,
fieldnames=[
'Country',
'Capital',
'Population',
'Area(km²)'
]
)
writer.writeheader()
writer.writerows(rows)
print('All done !')