亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Stack Exchange API 獲取答案的正文?

如何使用 Stack Exchange API 獲取答案的正文?

翻閱古今 2021-09-14 21:26:56
我正在使用StackAPI來獲得投票最多的問題和這些問題的投票最多的答案:-from stackapi import StackAPISITE = StackAPI('stackoverflow')SITE.max_pages=1SITE.page_size=10questions = SITE.fetch('questions', min=20, tagged='python', 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('Most Voted Answer ID :- {0}'.format(top_answer['items'][0]['answer_id']))現在使用這個answer_id我想得到那個答案的正文。我可以通過使用此 API 鏈接獲取其余詳細信息。
查看完整描述

1 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

您需要使用自定義過濾器來獲取問題/答案/帖子正文。


好消息是,您還可以使用自定義過濾器在獲取問題的同時獲取答案數據——無需再調用 API。


例如,如果您/questions使用過濾器調用路由:

? ? !*SU8CGYZitCB.D*(BDVIficKj7nFMLLDij64nVID)N9aK3GmR9kT4IzT*5iO_1y3iZ)6W.G*


你得到的結果如下:


"items": [ {

    "tags": ["python", "iterator", "generator", "yield", "coroutine"],

    "answers": [ {

        "owner": {"user_id": 8458, "display_name": "Douglas Mayle"},

        "is_accepted": false,

        "score": 248,

        "creation_date": 1224800643,

        "answer_id": 231778,

        "body": "<p><code>yield</code> is just like <code>return</code> - it returns what..."

        }, {

        "owner": {"user_id": 22656, "display_name": "Jon Skeet"},

        "is_accepted": false,

        "score": 139,

        "creation_date": 1224800766,

        "answer_id": 231788,

        "body": "<p>It's returning a generator. I'm not particularly familiar with Python, ..."

        }, {

        ...

    } ],

    "owner": {"user_id": 18300, "display_name": "Alex. S."},

    "is_answered": true,

    "accepted_answer_id": 231855,

    "answer_count": 40,

    "score": 8742,

    "creation_date": 1224800471,

    "question_id": 231767,

    "title": "What does the &quot;yield&quot; keyword do?"

    },

    ...

所以,改變這個:


questions = SITE.fetch('questions', min=20, tagged='python', sort='votes')

對于這樣的事情:


questions = SITE.fetch('questions', min=20, tagged='python', sort='votes', filter='!*SU8CGYZitCB.D*(BDVIficKj7nFMLLDij64nVID)N9aK3GmR9kT4IzT*5iO_1y3iZ)6W.G*')

然后相應地調整你的for循環。


查看完整回答
反對 回復 2021-09-14
  • 1 回答
  • 0 關注
  • 318 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號