2

I am using the stackAPI Python wrapper to interface with the stackexchange api. I am trying to fetch the top most popular questions above a vote count threshold; and for each of those questions the top answers above a certain vote count.

  SITE = StackAPI("stackoverflow")
  SITE.max_pages=2
  SITE.page_size=100
  questions = SITE.fetch('questions', min=10, sort='votes')
  for quest in questions['items']:
    if 'title' not in quest: continue
    quest_id = quest['question_id']
    title = quest['title']
    tags = []
    if 'tags' in quest:
      tags = quest['tags']
    #body = quest['body']
    body = ""
    if 'body' in quest:
      body = quest['body']

    answers = SITE.fetch('answers', id=[quest_id],min=10, sort='votes')
    for answer in answers['items']:
         _stuck here_

This is where I am stuck, how to fetch answers for the above question_id this query is returning some random answer_ids. How do fetch the question-< answers

Santino
  • 776
  • 2
  • 11
  • 29

2 Answers2

5

I have modified your code to get the top voted answer like this.

for quest in questions['items']:
    if 'title' not in quest or quest['is_answered'] == False:
        continue
    title = quest['title'] 
    print('Question :- {0}'.format(title))
    question_id = quest['question_id']
    print('Question ID :- {0}'.format(question_id))
    top_answer = SITE.fetch('questions/' + str(question_id) + '/answers', order = 'desc', sort='votes')
    print(top_answer)

If you want to get the accepted answer for the question you can get it this way:-

accepted_answer_id = quest['accepted_answer_id']
print('Accepted Answer ID :- {0}'.format(accepted_answer_id))

Stackoverflow uses this id as example below to genarate the url to that answer like this:-

answer = "https://stackoverflow.com/a/" + str(accepted_answer_id)
print(answer)

Hope this helps you !

Shashishekhar Hasabnis
  • 1,636
  • 1
  • 15
  • 36
3

You're using the question ID as the answer ID, but these IDs are totally unrelated.

Use the questions/{ids}/answers endpoint.

answers = SITE.fetch('answers/' + str(quest_id) + '/answers', min=10, sort='votes')
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi I tried your above suggestion, unfortunately no luck. This is my code: – Santino Jan 11 '18 at 05:59
  • ` answers = SITE.fetch('answers/' + str(11227809) + '/answers', min=2, sort='votes') #pprint.pprint(answers) answer_id_list = [] for answer in answers: #answ_title = answer['title'] answer_id_list.append(answer['answer_id']) print answer ` – Santino Jan 11 '18 at 06:00
  • Get a `no method` exception. Not sure what that means. – Santino Jan 11 '18 at 06:01
  • The error seems pretty self-explanatory, you (or something internal to one of the functions you called) tried to call a method that doesn't exist. What line got that error? – Barmar Jan 11 '18 at 16:25
  • http://api.stackexchange.com/docs/answers-on-questions#order=desc&min=2&sort=votes&ids=11227809&filter=default&site=stackoverflow&run=true shows that the URL should work – Barmar Jan 11 '18 at 16:28
  • It should be `for answer in answers['items']:` – Barmar Jan 11 '18 at 16:29