大概用法如下:
实际场景:
# 需求:Meet算法推荐中,18岁以下(含18岁)的用户不出现meet卡片中,注册新账号,看下user_meet表的status from knight_util.MysqlUtil import MysqlPool from knight_util.get_db_token import tk_ from knight_util.SendRequest import send_request from tqdm import tqdm mysql = MysqlPool() tk = tk_(2397).get('EPAL_WEB') def less_nineteen(num): """返回小于18的数据""" if num is None: pass else: return num < 18 def get_userid_age_dict(): """获取用户id和年龄""" headers = { "_tk_": tk, "content-type": "application/json", "user-agent": "Mozilla / 5.0(Windows NT 10.0;Win64;x64)" } data = {"limit": 30, "likeUserIds": [], "dislikeUserIds": [], "excludeUserIds": []} url = "https://test-community.gamesegirl.com/user/meets/match/v-3.6" response = send_request(url, headers, data) # print(response) if response['status'] == 'OK': response_length = len(response['content']['data']) # 接口返回的user_id和age user_info = {} for i in range(response_length): user_info.update( {response['content']['data'][i]['userId']: response['content']['data'][i]['age']} ) print(user_info) print(f"总共卡片数:{len(user_info.keys())}") null_value_list = [k for k, v in user_info.items() if v is None] print(null_value_list) # for k, v in user_info.items(): # if v is None: # null_value_list.append(k) # print(null_value_list) # 小于18岁的年龄 result = filter(less_nineteen, user_info.values()) print(list(result)) if __name__ == '__main__': get_userid_age_dict()