Created
October 8, 2024 14:44
-
-
Save overflowy/99e0d2314752c670fe59351387c57736 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import requests | |
from bs4 import BeautifulSoup | |
def export_to_csv(url: str): | |
r = requests.get(url) | |
soup = BeautifulSoup(r.text, "html.parser") | |
table = soup.find("table", {"id": "downloadTableEN"}) | |
headers = [header.text for header in table.find_all("th")] | |
if not headers: | |
headers = [header.text for header in table.find("tr").find_all("td")] | |
rows = [] | |
for row in table.find_all("tr")[1:]: # Skip the header row | |
cells = row.find_all("td") | |
row_data = [cell.text.strip() for cell in cells] | |
rows.append(row_data) | |
with open("output.csv", "w", newline="", encoding="utf-8") as file: | |
writer = csv.writer(file) | |
writer.writerow(headers) | |
writer.writerows(rows) | |
def main(): | |
export_to_csv("https://unstats.un.org/unsd/methodology/m49/overview") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment