我尝试使用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:\Users\Tasos\Desktop\test - Copy.py", line 8, in <module>
my_favourite_guy = so.user(2309097)
File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 626, in user
u, = self.users((nid,), **kw)
File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 631, in users
return self._get(User, ids, 'users', kw)
File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 621, in _get
return self.build(root, typ, coll, kw)
File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 598, in build
json = self._request(url, kw)
File "build\bdist.win-amd64\egg\stackexchange\__init__.py", line 570, in _request
json, info = request_mgr.json_request(url, new_params)
File "build\bdist.win-amd64\egg\stackexchange\web.py", line 120, in json_request
return (json.loads(req.data), req.info)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.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
压缩。在这种情况下,只需更新到最新版本
图书馆。
最佳答案
version on PyPI已经过时(2011年发布),并且仍使用API版本1.1,该版本为shut down。
Github codebase已更新为使用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()
调用获取下一个查询集,直到用完为止。关于python - Py-StackExchange提高valueError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24139250/