首页 > 解决方案 > 尝试从 API 获取文件时 Python 的 request() 返回响应 400

问题描述

这是我第一次弄乱 API。我编写了一些代码来使用“GET”调用从 USASpending 的 API 获取数据,它可以工作。我现在正在尝试从另一个仅具有“发布”功能的端点获取数据。下面的代码返回“400”响应。我不确定我需要更改什么才能生成输出。我不知道这是否重要,但此端点会生成一个 zip 文件。

import requests

payload = {"award_levels":["prime_awards"],"filters":{"award_types":["contracts","direct_payments","grants","idvs","loans","other_financial_assistance"],"agency":"United States Mint","date_type":"action_date","date_range":{"start_date":"2019-01-01","end_date":"2019-01-31"}},"columns":[],"file_format":"csv"}

response = requests.post('https://api.usaspending.gov/api/v2/bulk_download/awards/', params = payload).json()

提前致谢

更新:

这是我最终使用的。我将日期设置为动态的,因此我可以使用任务计划程序每月自动执行此拉动。如果要提取特定日期,请删除标头声明中的日期变量引用并运行。

pl_str1 = """{
"filters": {
"prime_award_types": [
"A",
"B",
"C",
"D",
"IDV_A",
"IDV_B",
"IDV_B_A",
"IDV_B_B",
"IDV_B_C",
"IDV_C",
"IDV_D",
"IDV_E",
"02",
"03",
"04",
"05",
"10",
"06",
"07",
"08",
"09",
"11"],
"agency": 54,
"date_type": "action_date","""
# Inserting date_range variable into API call
pl_str2 =  '"date_range": {"start_date":'  + date_3Months_prior +', "end_date":' + date_today + '}'
pl_str3 = """},
"columns": [],
"file_format": "csv"}"""

desired_payload = pl_str1 + pl_str2 + pl_str3

# $$$$$$$$$$$$$$$$$$$$$$$$$$$ REQUESTING DESIRED INFO FROM THE API HERE $$$$$$        
url = 'https://api.usaspending.gov/api/v2/bulk_download/awards/'
headers = {'Content-Type': 'application/json'}
resp = requests.post(url, headers=headers, data=desired_payload)
if resp.status_code == 200:
    print('success')
    print(resp.content)
else:
    print('fail')
    
# API returns a zip file; grabbing that   
# This will turn the API response into a string I can use regex on
test = resp.content.decode('UTF-8')
# Extracting url for zip we want to retrieve
test2 = re.findall(r"file_url\S+\.zip", test)[0]
test3 = re.findall(r"https\S+\.zip", test2)[0]

import zipfile
import io
import time
r = requests.get(test3)
# add sleep in case it takes a while for the API to return stuff; not sure if 
# but just in case
time.sleep(5)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall()

标签: pythonpython-requests

解决方案


params是 URL 中的查询参数。

例如

www.stackoverflow.com?parameter=test

这可以表示为

payload = {
     "parameter": "test"
}

POST 请求有一个body,这与从表单发送的数据相同,您正在发送一个body,您在请求模块函数.post中有一些处理正文的参数,您可以使用data=payloadjson=payload请参阅有关此的官方文档。


推荐阅读