python - 如何使用 Stack Exchange API 获取答案的正文?

标签 python python-3.x stackexchange-api

我正在使用StackAPI获得投票最多的问题以及这些问题投票最多的答案:-

from stackapi import StackAPI
SITE = StackAPI('stackoverflow')
SITE.max_pages=1
SITE.page_size=10

questions = 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 我想获取该答案的正文。 我可以使用 this API link 获取其余详细信息.

最佳答案

请参阅有关 Stack Apps 的这些帖子:

  1. Get questions with body and answers
  2. How to get Question/Answer body in the API response using filters?
  3. My filter is not returning any results. How to create a minimal filter?

您需要使用 custom filter获取问题/答案/帖子正文。

好消息是,您还可以使用自定义过滤器在获取问题的同时获取答案数据 - 无需稍后调用 API。

例如,如果您使用过滤器调用 /questions 路由:
!*SU8CGYZitCB.D*(BDVIficKj7nFMLLDij64nVID)N9aK3GmR9kT4IzT*5iO_1y3iZ)6W.G*

You get results like :

"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 循环。

关于python - 如何使用 Stack Exchange API 获取答案的正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747595/

相关文章:

python-3.x - 尝试在 S3 存储桶中保存 matplotlib 饼图时,空文件上传到 S3 存储桶

python - 为什么 + 运算符在创建列表时比 append() 慢得多?

python - 如何将具有不规则列的html代码转换为嵌套的json文件?

python - NetworkX g.neighbors(n) 时间复杂度

python - 如何设置kivy应用程序的最小窗口大小?

python - 无法让 Kivy 代码工作

javascript - 堆栈交换API : get post list from user

python - 是否值得使用 IMAP COMPRESS (DEFLATE)?

sql - Stack Overflow 查询以选择具有 3 个或更多答案的问题

node.js - Node : Getting paginated get requests