Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- import os
- import json
- url = 'https://en.wikipedia.org/wiki/ASEAN'
- filename = 'urban_areas.json'
- response = requests.get(url)
- soup = BeautifulSoup(response.text, 'html.parser')
- table = soup.find('table', class_='sortable wikitable plainrowheaders')
- countries_dictionary = {}
- for row in table.find_all('tr')[1:]:
- cols = row.find_all('td')
- city = row.find('th').text.strip()
- population = int(cols[0].text.strip().replace(',', ''))
- area = float(cols[1].text.strip().replace(',', ''))
- country = cols[2].text.strip()
- city_data = {
- 'City': city,
- 'Population': population,
- 'Area (km²)': area,
- 'Population Density': round(population / area, 2)
- }
- countries_dictionary.setdefault(country, []).append(city_data)
- if os.path.exists(filename):
- with open(filename, 'r') as file:
- existing_data = json.load(file)
- else:
- existing_data = {}
- if existing_data != countries_dictionary:
- with open(filename, 'w') as file:
- json.dump(countries_dictionary, file, indent=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement