Py-StackExchange抛出valueError异常



我尝试使用StackExchange API,我找到了Python的Py-StackExchange库。我是通过Windows的easy_install安装的。

代码如下:

from stackexchange import Site, StackOverflow
so = Site(StackOverflow)
my_favourite_guy = so.user(2309097)
print my_favourite_guy.reputation.format()
print len(my_favourite_guy.answers), 'answers'

错误如下:

Traceback (most recent call last):
  File "C:UsersTasosDesktoptest - Copy.py", line 8, in <module>
    my_favourite_guy = so.user(2309097)
  File "buildbdist.win-amd64eggstackexchange__init__.py", line 626, in user
    u, = self.users((nid,), **kw)
  File "buildbdist.win-amd64eggstackexchange__init__.py", line 631, in users
    return self._get(User, ids, 'users', kw)
  File "buildbdist.win-amd64eggstackexchange__init__.py", line 621, in _get
    return self.build(root, typ, coll, kw)
  File "buildbdist.win-amd64eggstackexchange__init__.py", line 598, in build
    json = self._request(url, kw)
  File "buildbdist.win-amd64eggstackexchange__init__.py", line 570, in _request
    json, info = request_mgr.json_request(url, new_params)
  File "buildbdist.win-amd64eggstackexchangeweb.py", line 120, in json_request
    return (json.loads(req.data), req.info)
  File "C:Python27libjson__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:Python27libjsondecoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:Python27libjsondecoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我在Wiki上看到了以下内容,但我没有使用任何代理,代码版本是最新的easy_install:

这可能是一些代理/路由器与请求混淆的结果头。可能是你的路由器/代理添加了请求头Gzip数据,但不解压缩它,并且您正在运行稍微旧的代码版本,不处理gzip压缩。在这种情况下,只需更新到最新版本的图书馆。

PyPI上的版本已经过时了(发布于2011年),仍然使用API版本1.1,该版本已经关闭。

Github代码库已经更新为使用API v2.2,直接安装:

pip install git+https://github.com/lucjon/Py-StackExchange

或使用easy_install,下载当前主zip:

easy_install https://github.com/lucjon/Py-StackExchange/archive/640eac1525baaf57474ddbc3be2b580f00e4f1e8.zip

要获得列出的答案,您需要调用.fetch():

print len(my_favourite_guy.answers.fetch()), 'answers'

这只获取答案的第一页:

>>> from stackexchange import Site, StackOverflow
>>> so = Site(StackOverflow)
>>> my_favourite_guy = so.user(2309097)
>>> print my_favourite_guy.reputation.format()
563
>>> print len(my_favourite_guy.answers.fetch()), 'answers'
19 answers
>>> my_favourite_guy = so.user(100297)
>>> print my_favourite_guy.reputation.format()
251.2k
>>> print len(my_favourite_guy.answers.fetch()), 'answers'
30 answers

上次我检查的时候,我有30多个答案。使用.extend_next()调用获取下一个查询集,直到用完。

相关内容

  • 没有找到相关文章

最新更新