You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何遍历MemberID并传入API请求参数,批量获取用户扩展信息

Fix: Fetch Extended Profile Info for All Search Results

Looks like the issue here is that you're only making one call to the extended profile API—after your loop finishes processing search results. Plus, you're overwriting the memberid variable each time through the loop instead of collecting all the IDs, so you end up only using a single ID for that one API call.

Here's how to adjust your code to fetch extended info for every user from your search results:

Option 1: Collect all IDs first, then fetch each profile

This approach stores all member IDs in a list first, then loops through that list to get each extended profile. Good if you need to reuse the list of IDs later.

import requests
import json

# Search for profiles
search_response = requests.post('https://api_search_for_profiles', headers=search_headers, data=search_params)
search_data = search_response.json()  # Skip unnecessary dumps/loads—.json() parses directly

# Collect all member IDs from search results
member_ids = []
for ps in search_data['data']['content']:
    member_id = str(ps['id'])
    member_ids.append(member_id)
    print(f'UserID: {ps["roomNo"]}')
    print(f'MemberID: {member_id}')
    print(f'Username: {ps["nickName"]}')
    print('---')

# Set up headers for profile API (only need to do this once)
profile_headers = {
    'x-auth-token': f'{token}',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'okhttp/3.11.0',
}

# Fetch extended info for each member ID
for member_id in member_ids:
    profile_params = {
        'id': '',
        'token': f'{token}',
        'memberId': member_id,  # Use the current ID from the loop
        'roomNo': ''
    }
    profile_response = requests.post('https://api_extended_profile_information', headers=profile_headers, data=profile_params)
    profile_data = profile_response.json()
    
    # Check if the API call succeeded (using your sample's code=0 success flag)
    if profile_data.get('code') == 0:
        pfd = profile_data['data']
        print(f"Extended Info for MemberID {member_id}:")
        print(f"UserID: {pfd['roomNo']}")
        print(f"Username: {pfd['nickName']}")
        print(f"Gender: {pfd['gender']}")
        print(f"Member Level: {pfd['memberLevel']}")
    else:
        print(f"Failed to fetch info for MemberID {member_id}: {profile_data.get('message', 'No error message provided')}")
    print('---')

Option 2: Fetch extended info immediately in the search loop

This is more efficient for large result sets (no need to store all IDs) since you process each search result and fetch its profile right away:

import requests
import json

# Search for profiles and fetch extended info on-the-fly
search_response = requests.post('https://api_search_for_profiles', headers=search_headers, data=search_params)
search_data = search_response.json()

# Set up profile API headers once
profile_headers = {
    'x-auth-token': f'{token}',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'okhttp/3.11.0',
}

for ps in search_data['data']['content']:
    member_id = str(ps['id'])
    # Print basic search info
    print(f'UserID: {ps["roomNo"]}')
    print(f'MemberID: {member_id}')
    print(f'Username: {ps["nickName"]}')
    
    # Fetch extended profile for this user
    profile_params = {
        'id': '',
        'token': f'{token}',
        'memberId': member_id,
        'roomNo': ''
    }
    profile_response = requests.post('https://api_extended_profile_information', headers=profile_headers, data=profile_params)
    profile_data = profile_response.json()
    
    if profile_data.get('code') == 0:
        pfd = profile_data['data']
        print(f"Extended Info: Gender={pfd['gender']}, Level={pfd['memberLevel']}")
    else:
        print(f"⚠️ Failed to get extended info: {profile_data.get('message', 'Unknown error')}")
    print('---')

Key Fixes Explained:

  1. Removed redundant JSON processing: requests.Response.json() already parses the JSON into a Python dictionary—no need to use json.dumps() followed by json.loads().
  2. Loop through each member ID: Instead of making one profile API call after the loop, we call it once per member ID inside a loop.
  3. Added basic error handling: Checks if the profile API returned a successful response (code=0) to avoid crashes if a request fails.
  4. Properly collected IDs: In Option 1, we use a list to store all IDs instead of overwriting a single variable each time.

内容的提问来源于stack exchange,提问作者aquatic7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 11:58:15