Market Data & Web Scraping
Intermediate
Fetch US SEC EDGAR Company Facts & 10-K Financial JSON via API in Python
Download official 10-K and 10-Q GAAP revenue, net income, and balance sheet history directly from the SEC EDGAR REST API in Python without API keys.
#requests#pandas
Problem Statement
Paid fundamental data APIs charge $50+/month for historical US financial statements, even though the SEC provides structured XBRL JSON endpoints completely free.
Formula / Core Logic
Queries https://data.sec.gov/api/xbrl/companyfacts/CIK{10_digit_cik}.json with a compliant User-Agent header.
Installation
pip install requests pandas
sec-edgar-company-filings-10k-scraper-python.py
import requests
import pandas as pd
def fetch_sec_company_concept(cik='0000320193', concept='NetIncomeLoss', user_email='quant@example.com'):
cik_padded = str(cik).zfill(10)
url = f'https://data.sec.gov/api/xbrl/companyconcept/CIK{cik_padded}/us-gaap/{concept}.json'
headers = {'User-Agent': f'PyQuantLibrary {user_email}'}
data = requests.get(url, headers=headers, timeout=15).json()
units = list(data['units'].keys())[0]
df = pd.DataFrame(data['units'][units])
annual = df[df['form'] == '10-K'].drop_duplicates(subset=['fy'], keep='last')
return annual[['fy', 'end', 'val', 'form']]
Get the Complete 100+ Python Quant & Automation Bundle
Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).
Buy Full Bundle on Gumroad ($19) →Frequently Asked Questions
Q: Why does the SEC API return 403 Forbidden without a custom User-Agent?
SEC EDGAR policy strictly requires a descriptive `User-Agent` header in the format `AppName ContactEmail` to prevent anonymous bot abuse.