특정 사용자의 데이터를 API를 통해 가져오고 싶습니다. 기본적으로, 해당 사용자가 포럼에 가입한 이후에 몇 개의 답변을 남겼는지 알고 싶습니다. 아래 코드를 사용했습니다(기본적으로 action_type=5인 모든 user_actions를 가져온 후, 해당 레코드 수를 출력했습니다). 최종 출력이 웹 UI에서 확인한 내용과 일치하지 않았습니다. 이 현상이 발생하는 이유를 설명해 주실 수 있을까요?
import requests, time
defaultHost = #your_default_host
# 인증용 헤더
headers = {
'Api-Key': #YOUR_API_KEY,
'Api-Username':#YOUR_API_USERNAME
}
# 오프셋을 매번 30씩 증가시킵니다. 응답 내 요소 수가 30보다 작으면 루프가 종료됩니다.
offset = 0
data_dict ={"user_actions":[]}
username = #username_for_which_we_want_to_fetch_the_data
# filter=5 # "user_replied_to_a_topic"용
while True:
time.sleep(0.2)
endpoint = f'https://{defaultHost}/user_actions.json?username={username}&offset={offset}&filter=5'
# 요청 실행
response = requests.get(endpoint,
headers=headers)
# 응답 확인
if response.status_code == 200:
data = response.json()
num_elements = len(data["user_actions"])
data_dict["user_actions"].extend(data["user_actions"])
offset+=30
if num_elements<30:
break
else:
print(f'Error: {response.status_code}')
break
print(len(data_dict["user_actions"]))