对 Python 来说相当陌生......在我的代码中与 for 循环作斗争,特别是 Key: 'topic_title' 的分配。
我不断收到“列表索引超出范围”错误。“solicitation_topics”处的 JSON 响应是嵌套的,所以我相信我需要传递索引,这在尝试直接从 python 终端访问时有效,但是在函数中我不断收到错误。任何帮助将不胜感激。
import requests, json
def get_solicitations():
# api-endpoint
URL = "https://www.sbir.gov/api/solicitations.json"
# defining a params dict for the parameters to be sent to the API
PARAMS = {"keyword": 'sbir'}
# sending get requfiest and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
# extracting data in json format
api_data = r.json()
# storing selected json data into a dict
solicitations = []
for data in api_data:
temp = {
'solicitation_title': data['solicitation_title'],
'program': data['program'],
'agency': data['agency'],
'branch': data['branch'],
'close_date': data['close_date'],
'solicitation_link': data['sbir_solicitation_link'],
'topic_title': data['solicitation_topics'][0]['topic_title'],
}
solicitations.append(temp)
return (solicitations)
慕妹3146593
相关分类