简体   繁体   English

Python无法解码JSON对象

[英]No JSON object could be decoded Python

I'm trying to get some data from this website . 我正在尝试从该网站获取一些数据。 I can enter 'text' and 'longest_only' parameters but when I pass 'ontologies' param, it says No JSON object could be decoded. 我可以输入'text'和'longest_only'参数,但是当我传递'ontologies'参数时,它说无法解码JSON对象。 Here's the complete URL 这是完整的URL

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

The argument is ontologies[] , since you can specify more than one. 参数是ontologies[] ,因为您可以指定多个。 Your request should be similar to the one that the online search uses: 您的请求应类似于在线搜索所使用的请求:

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

Simply execute the same search there, and use the developer tools option of your favorite browser to check what is the actual payload being sent. 只需在此处执行相同的搜索,然后使用您喜欢的浏览器的开发人员工具选项来检查实际发送的有效载荷是什么。

This is not an answer, but the only place I can show the error that I see when executing the sample code. 这不是答案,而是唯一可以显示在执行示例代码时看到的错误的地方。 I placed the code in a new module in main and run it in Python 3.4. 我将代码放在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'])

I receive the following error. 我收到以下错误。

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