基础 API 请求
在本练习中,您将构建一个 API 请求,以获取美国所有州的平均家庭规模和年龄中位数。数据来自 2010 年十年人口普查(Decennial Census)的 Summary File 1。
已为您导入 requests。
本练习是课程的一部分
使用 Python 分析美国人口普查数据
练习说明
- 为
year和dataset指定合适的字符串值,以从十年人口普查的 Summary File 1 获取 2010 年的数据 - 构造
get_vars,即要请求的人口普查变量列表,包含以下变量名:"NAME"、"P013001"(年龄中位数)、"P037001"(平均家庭规模) - 使用
print函数输出r.text
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Build base URL
HOST = "https://api.census.gov/data"
year = ____
dataset = ____
base_url = "/".join([HOST, year, dataset])
# Specify Census variables and other predicates
get_vars = ____
predicates = {}
predicates["get"] = ",".join(get_vars)
predicates["for"] = "state:*"
# Execute the request, examine text of response object
r = requests.get(base_url, params=predicates)
print(____)