2

I need to find the total number of answers submitted by a user on stackoverflow. I tried the following:

I got my json from the following:

https://api.stackexchange.com/users/2440312/answers?site=stackoverflow

To get the total number of answers by the user_id I did:

json_object["items"].length

This however always returns me 30 if the number of answers by the user_id is more than 30. Is there a way I can get the actual number of answers by the user?

The documentation says that users/user_id/answers/ should return the list of all answers by the user

2 Answers 2

3

It does return all the answers, but they're paged - by default you'll only get 30 at a time. You'll likely want to increase pagesize to 100, and then add the additional code necessary to make subsequent requests if has_more is set on the response wrapper.

So, for example, your first request would be like

https://api.stackexchange.com/users/2440312/answers?site=stackoverflow&pagesize=100&page=1

and if the response had had_more you'd need to request

https://api.stackexchange.com/users/2440312/answers?site=stackoverflow&pagesize=100&page=2

and so on until has_more returns false. Be careful about requesting too quickly or you'll be throttled.

1
  • thanks a lot Tim! Can you also add how should the url be with the 'page_size' option. Dec 11, 2014 at 14:57
2

If you just want the total, don't bother paging through the entire full result set.
Use the total property of the Common Wrapper Object. This will save lots of time and bandwidth and quota and doesn't require you to loop through the results just to count things.

The total property is not enabled by default, so you will need to Create a custom filter for your queries. (You should do this anyway, to speed your app up.)

Here's how it looks for that /users/{ids}/answers route:
/users/2440312/answers?site=stackoverflow&filter=!.UE8F0_EMkjmCAGv

Which currently yields:

{
  "has_more": false,
  "quota_max": 10000,
  "quota_remaining": 9993,
  "page": 1,
  "page_size": 30,
  "total": 41,
  "type": "answer"
}

Unfortunately, the API does not do subtotals. So if you fetch more than one user at a time, this streamlined method will not work. (EG: this two-user request yields total = 3663 (at least 37 pages of answers) with no way to tell which user has which without paging through your original-style query.)

2
  • Thanks Brock for the tip :) . Actually I also wanted to filter total number of accepted answers and unaccepted answers. Is there a short way to get this too? I am currently checking each answer and its is_accepted' key for this. Dec 12, 2014 at 18:42
  • Unfortunately no. API support for answer-search is still spotty and buggy. To determine how many answers are accepted or not, you must page through, as you are currently doing, or you can scrape the normal search page and parse the results display at the top. Dec 12, 2014 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .