StackOverflow API returns only 20 questions

104 views Asked by At

The following code returns only 20 questions/results. How can I retrieve the whole number of questions for that date?

base_url = 'https://api.stackexchange.com/2.3'
endpoint = '/questions'
params = {
    'site': 'stackoverflow',
    'tagged': tag,
    'fromdate': start_date,
    'todate': end_date,
    'filter': 'default'  # Use 'default' or specify your desired filter
}

response = requests.get(base_url + endpoint, params=params)
data = response.json()
1

There are 1 answers

16
safir On

according to page doc pagesize can be any value between 0 and 100 and defaults to 30. if with the default values you only get 20 questions, it's probably because there are only this many questions fitting your tag in the time span given (can't tell as it's not included) otherwise you would get 30 results and would need to paginate through the different pages of results with page param like so

base_url = 'https://api.stackexchange.com/2.3'
endpoint = '/questions'
page = 1
pagesize = 30
page_results = []
while (page == 1 or page_results["has_more"] == True) :
  params = {
    'site': 'stackoverflow',
    'page': page,
    'pagesize' : pagesize,
    'tagged': tag,
    'fromdate': start_date,
    'todate': end_date,
    'filter': 'default'  # Use 'default' or specify your desired filter
  }
  page_results = requests.get(base_url + endpoint, params=params).json()
  page +=1