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>para score1 1 历史 para2 score2 2 历史 表类>
与其在给定的列上使用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()