2014-06-10 44 views
2

我嘗試使用StackExchange API,並找到Python的Py-StackExchange庫。我通過Windows中的easy_install安裝它。Py-StackExchange引發一個值錯誤

下面是代碼:

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:

This is probably the result of some proxy/router mangling with request headers. It could be that your router/proxy adds headers requesting gzip data, but doesn't decompress it, and that you are running a slightly old version of the code which does not deal with gzip compression. In this case, just update to the latest version of the library.

+0

返回https://api.stackexchange.com/docs/api-v1-shutdown。 –

+0

@MartijnPieters這意味着該庫在使用舊的api版本之後暫時沒有用處? – Tasos

+0

PyPI版本確實沒用;改爲從GitHub安裝。 –

回答

2

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,下載最新的主拉鍊:

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()調用來獲取下一個查詢集,直到用完爲止。