我正在使用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_
这就是我卡住的地方,如何为上述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)
如果你想得到这个问题的接受答案,你可以这样得到:-
accepted_answer_id = quest['accepted_answer_id']
print('Accepted Answer ID :- {0}'.format(accepted_answer_id))
Stackoverflow使用此id作为下面的示例来生成该答案的URL,如下所示:-
answer = "https://stackoverflow.com/a/" + str(accepted_answer_id)
print(answer)
希望这对您有所帮助!
您使用问题 ID 作为答案 ID,但这些 ID 完全不相关。
使用questions/{ids}/answers
终结点。
answers = SITE.fetch('answers/' + str(quest_id) + '/answers', min=10, sort='votes')