instant-data-scraper-utils/bin/scrape-census.py
2024-07-16 18:08:36 -04:00

41 lines
1.3 KiB
Python
Executable file

#!/usr/bin/env python
import requests
import csv
import io
# Send a GET request to the US Census Bureau dataset URL
url = "https://www2.census.gov/programs-surveys/popest/datasets/2010-2019/cities/totals/sub-est2019_all.csv"
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Read the CSV content from the response
csv_content = io.StringIO(response.text)
# Create a CSV reader object
reader = csv.DictReader(csv_content)
# Open the output CSV file in write mode
output_file = "cities_data.csv"
with open(output_file, "w", newline="", encoding="utf-8") as output:
fieldnames = ["city", "state", "population"]
writer = csv.DictWriter(output, fieldnames=fieldnames)
# Write the header
writer.writeheader()
# Iterate over each row in the input CSV
for row in reader:
# Extract the required fields
city = row["NAME"]
state = row["STNAME"]
population = row["POPESTIMATE2019"]
# Write the data to the output CSV file
writer.writerow({"city": city, "state": state, "population": population})
print("Data extraction completed. Output saved as", output_file)
else:
print("Failed to fetch the dataset.")