从Census Data API获取数据失败,API密钥及JSON解析报错求助
问题
我在Anaconda和Jupyter Notebook中尝试使用Pandas获取Census Track数据,但持续报错。我的代码如下:
import pandas as pd import numpy as np import requests import geopandas as gpd year = '2021' variable = 'B09019_003E' with open (r"pathToKeyFile/census_api.txt", "r") as f: c = f.read(); url='https://api.census.gov/data/{0}/acs/acs5?get=NAME,{1}&for=tract:*&in=state:12%20county:057&key={2}'\ .format(year, variable, c) response = requests.request("GET",url) response.raise_for_status() print(response.json) temp = pd.DataFrame(response.json()[1:], columns=response.json()[0])
执行print(response.json)的输出为:
b'\n<html>\n <head>\n <title>Invalid Key</title>\n </head>\n <body>\n <p>\n A valid <em>key</em> must be included with each data API request.\n You included a key with this request, however, it is not valid.\n Please check your key and try again.\n </p>\n <p>\n If you do not have a key you may sign up for one <a href="key_signup.html">here</a>.\n </p>\n </body>\n</html>\n'
我的census_api.txt文件仅包含密钥。执行pd.DataFrame()时出现如下错误:
JSONDecodeError Traceback (most recent call last) File C:\ProgramData\anaconda3\lib\site-packages\requests\models.py:971, in Response.json(self, **kwargs) 970 try: ---> 971 return complexjson.loads(self.text, **kwargs) 972 except JSONDecodeError as e: 973 # Catch JSON-related errors and raise as requests.JSONDecodeError 974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError File C:\ProgramData\anaconda3\lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 343 if (cls is None and object_hook is None and 344 parse_int is None and parse_float is None and 345 parse_constant is None and object_pairs_hook is None and not kw): ---> 346 return _default_decoder.decode(s) 347 if cls is None: File C:\ProgramData\anaconda3\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w) 333 """Return the Python representation of ``s`` (a ``str`` instance 334 containing a JSON document). 335 336 """ ---> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 338 end = _w(s, end).end() File C:\ProgramData\anaconda3\lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx) 354 except StopIteration as err: ---> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end JSONDecodeError: Expecting value: line 2 column 1 (char 1) During handling of the above exception, another exception occurred: JSONDecodeError Traceback (most recent call last) Cell In[21], line 1 ----> 1 temp = pd.DataFrame(response.json()[1:], columns=response.json()[0]) 2 temp['state'] = temp['state'].astype('str') 3 temp['county'] = temp['county'].astype('str') File C:\ProgramData\anaconda3\lib\site-packages\requests\models.py:975, in Response.json(self, **kwargs) 971 return complexjson.loads(self.text, **kwargs) 972 except JSONDecodeError as e: 973 # Catch JSON-related errors and raise as requests.JSONDecodeError 974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError ---> 975 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: Expecting value: line 2 column 1 (char 1)
我直接打开请求URL也显示API密钥无效,但我确定密钥是刚获取的,请问有什么解决思路?
解决思路
- 清理密钥多余字符:读取txt文件时容易带入换行符、空格或制表符,读取后用
strip()处理确保密钥纯净:with open(r"pathToKeyFile/census_api.txt", "r") as f: c = f.read().strip(); - 等待密钥激活:新获取的Census API密钥通常需要几分钟完成激活,稍等片刻后再重试请求。
- 手动验证密钥:直接复制密钥到浏览器的请求URL中测试,确认密钥本身没有输入错误。
- 检查URL拼接结果:打印拼接后的
url变量,查看密钥部分是否完整、无额外转义字符或截断。 - 先判断响应类型:当API返回错误HTML页面时,调用
response.json()必然触发解码错误,建议先做判断:if response.headers.get('Content-Type') == 'application/json': data = response.json() else: print("API返回非JSON内容:", response.text) - 校验API参数合法性:确认年份
2021、变量B09019_003E、州代码12和郡代码057是否属于ACS 5年数据的可用范围,避免因参数错误导致的密钥验证失败假象。
内容的提问来源于stack exchange,提问作者Mehdi Monemi
相关产品推荐
相关产品推荐

