-2

I am trying to pull specific questions on SO from a list of URLs using the stackapi Python library. I have been going through the documentation and some of the answers around similar questions on StackOverflow. All of them say that the API only allows you to pull questions between a fixed start-end time. Post that you are able to pull answers/comments.

SITE = StackAPI('stackoverflow')
questions = SITE.fetch('questions', fromdate=1457136000, todate=1457222400, min=20, tagged='python', sort='votes')
questions
{
    'backoff': 0,
    'has_more': False,
    'items': [
                {
                    u'accepted_answer_id': 35818398,
                    u'answer_count': 2,
                    u'creation_date': 1457186774,
                    u'is_answered': True,
                    u'last_activity_date': 1457246584,
                    u'last_edit_date': 1457200889,
                    u'link': u'http://stackoverflow.com/questions/35815093/sine-calculation-orders-of-magnitude-slower-than-cosine',
                    u'owner': {u'accept_rate': 80,
                               u'display_name': u'Finwood',
                               u'link': u'http://stackoverflow.com/users/1525423/finwood',
                               u'profile_image': u'https://i.stack.imgur.com/xkRry.png?s=128&g=1',
                               u'reputation': 1575,
                               u'user_id': 1525423,
                               u'user_type': u'registered'},
                    u'question_id': 35815093,
                    u'score': 22,
                    u'tags': [u'python', u'numpy', u'scipy', u'signal-processing'],
                    u'title': u'sine calculation orders of magnitude slower than cosine',
                    u'view_count': 404
                }
             ],
    'page': 1,
    'quota_max': 300,
    'quota_remaining': 171,
    'total': 0
}

I see another way of pulling badges/answers based on an ID. Is there an easy way to get the ID of a question from a URL? And is there a way of getting the single question, its comments and its answers then directly or indirectly (URL > ID > CONTENT)?

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • 1
    Fetching both the question, its answers and its comments cannot be done with 3 API calls. You need to use the following three methods: [`/questions/{ids}`](https://api.stackexchange.com/docs/questions-by-ids), [`/questions/{ids}/answers`](https://api.stackexchange.com/docs/answers-on-questions) and [`/questions/{ids}/comments`](https://api.stackexchange.com/docs/comments-on-questions). Also note that `fromdate` and `todate` params are optional, and I wouldn't recommend them unless you're sure you want them. – double-beep Aug 31 '20 at 04:26
  • 1
    For getting the *question* id of the URL, you can use `url.split('/')[4]`. If you need the question's/answer's comment's content (in some methods it is not returned by default), you need to use [filters](https://api.stackexchange.com/docs/filters). – double-beep Aug 31 '20 at 04:32

1 Answers1

1

You cannot do this in just one API call. Instead, you should use /questions/{ids}, /questions/{ids}/answers and /questions/{ids}/comments methods so as to achieve your goal.

Here's how it should look like:

from stackapi import StackAPI
QUESTION_IDS = [4] # For example
SITENAME = 'stackoverflow'
SITE = StackAPI(SITENAME)

question = SITE.fetch('questions/{ids}', ids=QUESTION_IDS, filter='withbody')
answers = SITE.fetch('questions/{ids}/answers', ids=QUESTION_IDS, filter='withbody')
comments = SITE.fetch('questions/{ids}/comments', ids=QUESTION_IDS, filter='withbody')
print(question, answers, comments)

where QUESTION_IDS holds an array with the question ids you have collected. If you have trouble finding which answer/comment belongs to which question, then look for the question_id field in the objects returned.

double-beep
  • 5,031
  • 17
  • 33
  • 41