调get接口
import requests
url = 'https://接口地址'
json_str = requests.get(url,params = {'key': key})
循环处理dataframe数据
for index, row in df.iterrows():
print (row['col'])
数据加密
getMd5 = hashlib.md5()
getMd5.update(str(get).encode(encoding='utf-8'))
key= getMd5.hexdigest()
返回结果转成json串
# response结果转json串使用.json()和json.loads()函数
# 第一种方法
result_json = json_str.json()
# 第二种方法
result_json = json.loads(json_str.text)
判断dict字典中的key值是否存在
#has_key方法在python2中可以使用,在python3中删除了。
比如:
if dict.has_key(word):
改为:
if word in dict:
#判断键值是否存在
if 'key1' in test1:
dataKey1 = test1['key1']
else:
dataKey1 = pd.DataFrame()
拼接dataframe
dataFull=dataKey1.append(dataKey2,ignore_index=True).append(dataKey3,ignore_index=True)
追加写入csv,可以不用最后跑完再写入,避免中间程序退出,结果不能正常写csv
# 追加写入csv
dataFull.to_csv("result.csv", mode='a', header=False,encoding="utf-8")
调用失败判断
if 'data' in result_json:
test1 = result_json['data']
get请求超时处理
http://docs.python-requests.org/en/master/user/quickstart/
异常处理
try:
except:
完整代码
#接口请求requests
import requests
import json
import pandas as pd
import hashlib
# 读取excel文件
data = pd.read_excel('C:/Users/Administrator/文件路径', encoding="utf-8")
# print (data)
#转成dataframe的形式
df = pd.DataFrame(data)
# print (df)
#存入结果
result=pd.DataFrame()
#循环读取数据处理
for index, row in df.iterrows():
get = row['col']
getMd5 = hashlib.md5()
getMd5.update(str(get).encode(encoding='utf-8'))
key= getMd5.hexdigest()
# print(userid)
url = 'https://接口地址'
json_str = requests.get(url,params = {'key': key})
result_json = json_str.json()
test1 = result_json['data']
# 打印返回的json
print (result_json)
# 判断键值是否存在
if 'key1' in test1:
dataKey1 = test1['key1']
else:
dataKey1 = pd.DataFrame()
if 'key2' in test1:
dataKey2 = test1['key2']
else:
dataKey2 = pd.DataFrame()
if 'key3' in test1:
dataKey3 = test1['key3']
else:
dataKey3 = pd.DataFrame()
dataKey1 = pd.DataFrame(dataKey1)
dataKey2= pd.DataFrame(dataKey2)
dataKey3 = pd.DataFrame(dataKey3)
# 结果拼接
dataFull=dataKey1.append(dataKey2,ignore_index=True).append(dataKey3,ignore_index=True)
for index, row in dataFull.iterrows():
dataFull['get']=get
# print(dataFull)
result = result.append(dataFull,ignore_index=True)
print(result)
result.to_csv ("result.csv" , encoding = "utf-8")