0

I am using StackAPI to extract the python related question, answers, and comments. I use following code to extract the questions related to Python.

from stackapi import StackAPI
SITE = StackAPI('stackoverflow')
questions = SITE.fetch('questions', tagged='python', fromdate=from_date, todate=today,filter='withbody')

Then I extract the question_id list from the retrieved questions to get the answers related to retrieve question

quest_ids= []
for quest in questions['items']:
    quest_ids.insert(len(quest_ids),quest['question_id'])

All question related ids are in quest_ids. Now I use quest_ids list to retrieve the answers of those question by following code:

answers =SITE.fetch('questions/{ids}/answers',ids=quest_ids,filter='withbody')

However, when I run the above code, I get the following errors:

JSONDecodeError                           Traceback (most recent call last) ~/anaconda3/lib/python3.6/site-packages/stackapi/stackapi.py in
fetch(self, endpoint, page, key, filter, **kwargs)
    189                 response.encoding = 'utf-8-sig'
--> 190                 response = response.json()
    191             except ValueError as e:

~/anaconda3/lib/python3.6/site-packages/requests/models.py in
json(self, **kwargs)
    896                     pass
--> 897         return complexjson.loads(self.text, **kwargs)
    898 

~/anaconda3/lib/python3.6/json/__init__.py in loads(s, encoding, cls,
object_hook, parse_float, parse_int, parse_constant,
object_pairs_hook, **kw)
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:

~/anaconda3/lib/python3.6/json/decoder.py in decode(self, s, _w)
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()

~/anaconda3/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

StackAPIError                             Traceback (most recent call
last) <ipython-input-43-8d66d5fd3e65> in <module>()
----> 1 answers =SITE.fetch('questions/{ids}/answers',ids=temp,filter='withbody')

~/anaconda3/lib/python3.6/site-packages/stackapi/stackapi.py in
fetch(self, endpoint, page, key, filter, **kwargs)
     190                 response = response.json()
     191             except ValueError as e:
--> 192                 raise StackAPIError(self._previous_call, str(e), str(e), str(e))
    193 
    194             try:
double-beep
  • 5,031
  • 17
  • 33
  • 41
user2293224
  • 2,128
  • 5
  • 28
  • 52

2 Answers2

0

You can only call /questions/{ids}/answers with 1 id per time.

So, if you'd like to fetch Python questions for, say 2020-05-09 to 2020-05-10, here's what I'd suggest:

from stackapi import StackAPI
import time

SITE = StackAPI('stackoverflow')
questions = SITE.fetch('questions', tagged='python', fromdate='1588982400', todate='1589068800', filter='withbody')
for quest in questions['items']:
    answers = SITE.fetch('questions/{ids}/answers', ids=[quest['question_id']], filter='withbody')
    print(answers) # do whatever you want with those

Since you're making a lot of requests, you may want to (if you haven't done so) register your application to get an API , which increases the number of requests you can make (quota) from 300 to 10,000.

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

I had a similar problem. In my case, json turned out to be corrupted. It used characters like '\ xa0' and '\\ n'. They had to be replaced.

text = text.replace (u '\ xa0', u '') .replace (u '\\\\ n', u '')

I found the error using the site https://jsonformatter.org/json-editor, which showed me the location of the error.