繁体   English   中英

Python无法解码JSON对象

[英]No JSON object could be decoded Python

我正在尝试从该网站获取一些数据。 我可以输入'text'和'longest_only'参数,但是当我传递'ontologies'参数时,它说无法解码JSON对象。 这是完整的URL

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

参数是ontologies[] ,因为您可以指定多个。 您的请求应类似于在线搜索所使用的请求:

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

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

这不是答案,而是唯一可以显示在执行示例代码时看到的错误的地方。 我将代码放在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM