簡體   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