如何从API响应中提取值


import pandas as pd
import requests
import json

def getAPIreturn(para):
url = "http://localhost/search?name={}".format(para)
try:
return_json = json.loads(requests.get(url).text)
response = return_json['data'] # get the first response string
print (type(response))
except:
response = "" 
return response
data = pd.read_csv("input.csv") 
data['return'] = data['para'].apply(getAPIreturn)
data.to_csv("output.csv",index=False)

以上代码将读取csv文件,并读取名为input的第一列以调用API作为参数,然后获取data并保存到名为return的第二列中,并输出csv文件。

如果我想从响应中选择多个值该怎么办?例如,score,count,status,并保存回CSV格式?

{
"data": [
{
"score": 1234
},
{
"count": 2
},
{
"id": "1234",
"Section": "abc",
"DisplayName": "hihi",
"status": "History"
}
]
}

CSV文件的首选结果:

<表类>输入得分数状态tbody><<tr>parascore11历史para2score22历史

与其在给定的列上使用apply,还不如在整个数据框上使用apply并传递axis=1。您的函数将每行调用一次。然后可以根据需要使用来自请求的结果更新该行,例如:

import pandas as pd
import requests
import json
def getAPIreturn(row):
para = row['para']
url = f"http://localhost/search?name={para}"

try:
return_json = json.loads(requests.get(url).text)
data = return_json['data']

score = data[0]['score']
count = data[1]['count']
status = data[2]['status']
except:
score = None
count = None
status = None

row['score'] = score
row['count'] = count
row['status'] = status

return row
data = pd.read_csv("input.csv") 
data = data.apply(getAPIreturn, axis=1)
data.to_csv("output.csv", index=False)

你也可以这样写:

return_json = requests.get(url).json()

相关内容

  • 没有找到相关文章

最新更新