Leg*_*ack 3 python api python-requests
我无法弄清楚如何使用python urllib或请求正确调用此api.
让我给你现在的代码:
import requests
url = "http://api.cortical.io:80/rest/expressions/similar_terms?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false"
params = {"positions":[0,6,7,29]}
headers = { "api-key" : key,
"Content-Type" : "application/json"}
# Make a get request with the parameters.
response = requests.get(url, params=params, headers=headers)
# Print the content of the response
print(response.content)
Run Code Online (Sandbox Code Playgroud)
我甚至在其余参数中添加了params变量:
url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
"retina_name":"en_associative",
"start_index":0,
"max_results":1,
"sparsity":1.0,
"get_fingerprint":False,
"positions":[0,6,7,29]}
Run Code Online (Sandbox Code Playgroud)
我收到此消息:
已记录内部服务器错误@ Sun Apr 01 00:03:02 UTC 2018
所以我不确定我做错了什么.你可以在这里测试他们的api,但即使进行测试,我也无法弄明白.如果我去http://api.cortical.io/,单击Expression选项卡,单击POST/expressions/similar_terms选项,然后粘贴{"positions":[0,6,7,29]}正文文本框并点击按钮,它会给你一个有效的回复,因此他们的API没有任何问题.
我不知道我做错了什么.你能帮助我吗?
问题是你在params字典中混合查询字符串参数和发布数据.相反,您应该params为查询字符串数据使用参数,并json为帖子正文数据使用参数(因为内容类型为json).
使用该json参数时,默认情况下Content-Type标头为'application/json'.
此外,当响应为json时,您可以使用该.json方法获取字典.
一个例子,
import requests
url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
"retina_name":"en_associative",
"start_index":0,
"max_results":1,
"sparsity":1.0,
"get_fingerprint":False
}
data = {"positions":[0,6,7,29]}
r = requests.post(url, params=params, json=data)
print(r.status_code)
print(r.json())
Run Code Online (Sandbox Code Playgroud)
200
[{'term': 'headphones', 'df': 8.991197733061748e-05, 'score': 4.0, 'pos_types': ['NOUN'], 'fingerprint': {'positions': []}}]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9347 次 |
| 最近记录: |