没有JSON对象可以解码Python

时间:2015-01-14 06:12:02

标签: python json post parameters get

我试图从这个website获取一些数据。我可以输入文字'和“最长的”#39;参数,但当我通过'本体' param,它说没有JSON对象可以被解码。这是完整的网址

http://data.bioontology.org/annotator?text=lung cancer,bone marrow&ontologies=NCIT&longest_only=true 
我使用的是Python 2.7

2 个答案:

答案 0 :(得分:1)

参数为ontologies[],因为您可以指定多个参数。您的请求应与online search使用的请求类似:

text=lung+cancer%2Cbone+marrow&ontologies%5B%5D=NCIT&longest_only=true&raw=true

只需在那里执行相同的搜索,并使用您喜欢的浏览器的开发人员工具选项来检查发送的实际有效负载是什么。

答案 1 :(得分:0)

这不是一个答案,但我唯一可以在执行示例代码时显示错误的地方。我将代码放在main的新模块中,并在Python 3.4中运行。

import requests

if __name__ == '__main__':
    url = 'http://bioportal.bioontology.org/annotator'
    params = {
        'text': 'lung cancer,bone marrow',
        'ontologies': 'NCIT',
        'longest_only': 'true'
    }

    session = requests.Session()
    session.get(url)

    response = session.post(url, data=params)
    data = response.json()

    # get the annotations
    for annotation in data['annotations']:
        print (annotation['annotatedClass']['prefLabel'])

我收到以下错误。

Traceback (most recent call last):
  File "/Users/.../Sandbox/Ontology.py", line 21, in <module>
    data = response.json()
  File "/Users/erwin/anaconda/lib/python3.4/site-packages/requests/models.py", line 799, in json
    return json.loads(self.text, **kwargs)
  File "/Users/erwin/anaconda/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/Users/erwin/anaconda/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/erwin/anaconda/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)