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

2 Answers 2

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 !

Sign up to request clarification or add additional context in comments.

Comments

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')

6 Comments

Hi I tried your above suggestion, unfortunately no luck. This is my code:
` 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 `
Get a no method exception. Not sure what that means.
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?
api.stackexchange.com/docs/… shows that the URL should work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.