How to Scrape US Census Data: Complete Guide for 2026
Tutorials

How to Scrape US Census Data: Complete Guide for 2026

Learn how to scrape US Census data ethically and efficiently using Python, Node.js, and AlterLab's API. Handle anti-bot protections and extract structured data.

H
Herald Blog Service
4 min read
0 views

AlterLab handles this automaticallyscrape any URL with one API call. No infrastructure required.

Try it free

This guide covers extracting publicly accessible data. Always review a site's robots.txt and Terms of Service before scraping.

TL;DR

To scrape US Census data in 2026, use AlterLab's API with automatic tier escalation. Start with T1 for static pages, then let the API promote to T3/T4 as needed for anti-bot protections. Extract structured data via CSS selectors or Cortex AI for typed JSON output.

Why collect government data from US Census?

Government data provides reliable foundations for analysis. Common use cases include:

  • Market researchers tracking demographic shifts for product expansion planning
  • Analysts correlating housing statistics with economic indicators
  • Developers building public policy tools that require authoritative population data

Technical challenges

US Census implements standard anti-bot measures: rate limiting by IP, User-Agent validation, and JavaScript challenges on dynamic pages. Raw HTTP requests often fail with 403 or 429 responses. AlterLab's Smart Rendering API automatically handles these via rotating residential proxies, realistic browser fingerprints, and tiered rendering approaches—escalating only when necessary to avoid overpaying for simple pages.

Quick start with AlterLab API

See the Getting started guide for SDK installation. Below are examples for scraping a public Census page showing median household income data.

Python example:

Python
import alterlab

client = alterlab.Client("YOUR_API_KEY")
response = client.scrape("https://www.census.gov/programs-surveys/acs/")
print(response.text)

Node.js example (MUST include this):

JAVASCRIPT
import { AlterLab } from "alterlab";

const client = new AlterLab({ apiKey: "YOUR_API_KEY" });
const response = await client.scrape("https://www.census.gov/programs-surveys/acs/");
console.log(response.text);

cURL example:

Bash
curl -X POST https://api.alterlab.io/v1/scrape \
  -H "X-API-Key: YOUR_KEY" \
  -d '{"url": "https://www.census.gov/programs-surveys/acs/"}'

Extracting structured data

For the ACS program page, target these visible elements:

  • Program title: h1.usa-heading-lg
  • Description: .usa-prose p:nth-of-type(1)
  • Last updated: .usa-tag (contains "Updated" text)

Use AlterLab's selector API to extract these:

Python
import alterlab

client = alterlab.Client("YOUR_API_KEY")
result = client.scrape(
    url="https://www.census.gov/programs-surveys/acs/",
    selectors={
        "title": "h1.usa-heading-lg",
        "description": ".usa-prose p:nth-of-type(1)",
        "updated": ".usa-tag:contains('Updated')"
    }
)
print(result.data)

Structured JSON extraction with Cortex

For typed data extraction without manual selectors, use Cortex. This example extracts key metrics from a Census data table:

Python
import alterlab

client = alterlab.Client("YOUR_API_KEY")
result = client.extract(
    url="https://data.census.gov/table/ACSST1Y2022.S1901?q=median+income",
    schema={
        "type": "object",
        "properties": {
            "median_household_income": {"type": "number"},
            "margin_of_error": {"type": "number"},
            "geography": {"type": "string"},
            "year": {"type": "integer"}
        }
    }
)
print(result.data)  # Typed JSON output: {"median_household_income": 74580, ...}

Cost breakdown

US Census pages typically require T3 (Stealth) due to header validation and light JS challenges. AlterLab auto-escalates tiers—you start at T1 and only pay for the successful tier. See AlterLab pricing for full details.

TierUse CaseCost per RequestCost per 1,000Requests per $1
T1 — CurlStatic HTML, no JS needed$0.0002$0.205,000
T2 — HTTPStandard pages with headers$0.0003$0.303,333
T3 — StealthProtected pages, anti-bot active$0.002$2.00500
T4 — BrowserFull JS rendering required$0.004$4.00250
T5 — CAPTCHACAPTCHA solving + JS rendering$0.02$20.0050

Note: For most Census pages, expect T3 usage ($0.002/request). Auto-escalation prevents wasted spend—T1/T2 attempts that fail don't incur charges.

99.2%Success Rate
1.2sAvg Response
$0.002Per Request (T3)

Best practices

  • Respect census.gov/robots.txt: disallow paths like /api/ but allow /programs-surveys/
  • Implement rate limiting: 1 request/second per IP to avoid triggering protections
  • Handle dynamic content: AlterLab's T4 tier renders JS-heavy tables; use wait_for_network_idle=true
  • Rotate User-Agents: AlterLab does this automatically via proxy pools
  • Monitor responses: check for 429/403 and adjust concurrency

Scaling up

For large datasets:

  1. Batch requests: Use AlterLab's /batch endpoint for 100-url chunks
  2. Schedule recurring scrapes: Cron expressions via AlterLab's Scheduling API
  3. Store results: Stream JSON output directly to data warehouses
  4. Handle pagination: Census tables often use &page= parameters)
Share

Was this article helpful?

Frequently Asked Questions

Scraping publicly accessible data from US Census is generally permissible under laws like hiQ v LinkedIn, but you must review census.gov's robots.txt and Terms of Service, implement rate limiting, and avoid private or restricted data.
US Census employs standard anti-bot protections including rate limiting and header validation. AlterLab handles these via automatic proxy rotation, header management, and smart rendering tiers.
Costs range from $0.0002 per request for static HTML (T1) to $0.004 for full browser rendering (T4), with AlterLab's auto-escalation ensuring you only pay for the successful tier. See our pricing table for details.