我正在尝试从这个网站获取一些数据。我可以输入"文本"和"longest_only"参数,但是当我传递"本体"参数时,它说无法解码 JSON 对象。这是
癌症的完整网址 http://data.bioontology.org/annotator?text=lung 骨髓&本体=NCIT&longest_only=真我正在使用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)