如何使用 StackExchange API 获取已接受答案的降价内容?
How can I get the markdown content of an accepted answer with the StackExchange API?
我正在尝试获取发出此请求的已接受答案的降价正文:
假设您知道问题 ID,您需要向 /questions/{ids}
发出 GET 请求并获取 accepted_answer_id
属性。然后,使用您已有的答案 ID 向 /answers/{ids}
发出另一个 GET 请求。使用适当的 filter,您可以将答案正文提取为降价 (body_markdown
) 或 HTML (body
) .
这里是 Python 代码:
from stackapi import StackAPI
question_id = 54428242 # a random question
sitename = "Whosebug"
# only include accepted_answer_id
question_filter = "!9bOY8fLl6"
# only include body and body_markdown
answer_filter = "!-)QWsboN0d_T"
SITE = StackAPI(sitename)
question = SITE.fetch("questions/{ids}",
ids = question_id,
filter = question_filter)
accepted_answer_id = question["items"][0]["accepted_answer_id"]
answer = SITE.fetch("answers/{ids}",
ids = accepted_answer_id,
filter = answer_filter)
answer_info = answer["items"][0]
print("Answer's HTML body: ",answer_info["body"])
print("Answer's markdown body: ", answer_info["body_markdown"])
代码使用了StackAPI library (docs)。您可能还注意到我使用过滤器将 return 对象的属性限制为我需要的属性(并且仅限于那些属性)。我建议你也这样做。
参见:
我正在尝试获取发出此请求的已接受答案的降价正文:
假设您知道问题 ID,您需要向 /questions/{ids}
发出 GET 请求并获取 accepted_answer_id
属性。然后,使用您已有的答案 ID 向 /answers/{ids}
发出另一个 GET 请求。使用适当的 filter,您可以将答案正文提取为降价 (body_markdown
) 或 HTML (body
) .
这里是 Python 代码:
from stackapi import StackAPI
question_id = 54428242 # a random question
sitename = "Whosebug"
# only include accepted_answer_id
question_filter = "!9bOY8fLl6"
# only include body and body_markdown
answer_filter = "!-)QWsboN0d_T"
SITE = StackAPI(sitename)
question = SITE.fetch("questions/{ids}",
ids = question_id,
filter = question_filter)
accepted_answer_id = question["items"][0]["accepted_answer_id"]
answer = SITE.fetch("answers/{ids}",
ids = accepted_answer_id,
filter = answer_filter)
answer_info = answer["items"][0]
print("Answer's HTML body: ",answer_info["body"])
print("Answer's markdown body: ", answer_info["body_markdown"])
代码使用了StackAPI library (docs)。您可能还注意到我使用过滤器将 return 对象的属性限制为我需要的属性(并且仅限于那些属性)。我建议你也这样做。
参见: