Stackexchange API:获取特定堆栈的答案post

Stackexchange API: fetch answers to a specific stack post

我正在使用 stackAPI Python 包装器与 stackexchange api 交互。我正在尝试获取超过投票计数阈值的最热门问题;对于每个问题,超过一定票数的最佳答案。

  SITE = StackAPI("Whosebug")
  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_

这就是我卡住的地方,如何获取上述问题的答案question_id 此查询返回一些随机 answer_ids。如何获取 question-< answers

您正在使用问题 ID 作为答案 ID,但这些 ID 完全不相关。

使用 questions/{ids}/answers 端点。

answers = SITE.fetch('answers/' + str(quest_id) + '/answers', min=10, sort='votes')

我已经修改了您的代码以获得像这样的最高投票答案。

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)

如果您想获得该问题的公认答案,您可以通过以下方式获得:-

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

Whosebug 使用此 ID 作为下面的示例来生成 url 到该答案,如下所示:-

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

希望对您有所帮助!