San*_*ino 2 python python-2.7 stackexchange-api
我正在使用stackAPI Python 包装器与 stackexchange api 进行交互。我正在尝试获取高于投票计数阈值的最热门问题;对于每个问题,超过一定票数的最佳答案。
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_
Run Code Online (Sandbox Code Playgroud)
这就是我陷入困境的地方,如何获取上述 Question_id 的答案,此查询返回一些随机的answer_ids。如何获取question-< answers
我已经修改了您的代码以获得这样的投票最高的答案。
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)
Run Code Online (Sandbox Code Playgroud)
如果您想获得该问题的公认答案,可以通过以下方式获得:-
accepted_answer_id = quest['accepted_answer_id']
print('Accepted Answer ID :- {0}'.format(accepted_answer_id))
Run Code Online (Sandbox Code Playgroud)
Stackoverflow 使用此 id(如下例)来生成该答案的 url,如下所示:-
answer = "https://stackoverflow.com/a/" + str(accepted_answer_id)
print(answer)
Run Code Online (Sandbox Code Playgroud)
希望这对您有帮助!
归档时间: |
|
查看次数: |
2369 次 |
最近记录: |