I'm trying to get the markdown body of an accepted answer making this request:
Asked
Active
Viewed 104 times
0
-
`accepted_answer_id` – Daniel A. White Sep 11 '21 at 20:59
-
Already have that. But how to get the text? What request should I make? – HyperNight Sep 11 '21 at 21:12
-
For example, should I use /answer/{accepted_answer_id}? I want to get the contents of it in markdown syntax. – HyperNight Sep 12 '21 at 10:18
-
That's irrelevant, but I'm using Python's requests module. Already fixed this, let me create a comment. – HyperNight Sep 13 '21 at 17:23
2 Answers
1
Assuming you know the question id(s), you need to make a GET request to /questions/{ids} and get the accepted_answer_id property. Then, make another GET request to /answers/{ids} using the answer id you already have. With an appropriate filter, you can extract the body of an answer as markdown (body_markdown) or as HTML (body).
Here is the Python code:
from stackapi import StackAPI
question_id = 54428242 # a random question
sitename = "stackoverflow"
# 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"])
The code uses the StackAPI library (docs). You might also have noticed that I use filters to limit the return object's properties to the ones I need (and only those). I suggest you do the same.
See:
double-beep
- 4,567
- 13
- 30
- 40
0
Fixed by using this request: https://api.stackexchange.com/2.3/answers/{answer_id}?order=desc&sort=activity&site=stackoverflow&filter=withbody
HyperNight
- 27
- 7