pythonpython-3.xstackexchange-apistackexchange

Retrieving text body of answers and comments using Stackexchange API


I am trying to retrieve the questions, comments on questions, and answers of questions related to Python from stack overflow using Stack exchange API. I want to extract all information including body of text of questions, comments, and answers. For extracting questions, I am using following code:

questions = SITE.fetch('questions', tagged='python', fromdate=from_date, todate=today,filter='!9YdnSIN*P')

This filter returns all information related to question including text body of question. However, when I use the same filter to extract the answer of those questions, I don't get text body of answers. Here is the code:

answers = SITE.fetch('questions/{ids}/answers', ids=[59239886],filter='!9YdnSIN*P')

When I change the value of filter to

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

it started retrieving the text body of answer but it lost other vital information (tags) such as 'question_id' that shows answer related to question. Same problem holds for retrieving information related to comments on questions.

Could anyone guide me how can I get the text body of answers and comments without losing vital information?


Solution

  • Just use withbody filter.

    Example:

    from pprint import pprint
    
    from stackapi import StackAPI
    
    SITE = StackAPI('stackoverflow')
    
    pprint(SITE.fetch('questions/{ids}', ids=[59239886], filter='withbody'))
    pprint(SITE.fetch('questions/{ids}/answers', ids=[59239886], filter='withbody'))
    

    Using this filter it outputs all bodies and tags and everything