0
简短版本: 此Python请求不起作用。 Javascript版本的确。为什么?为什么Python请求库不能复制这个Ajax调用?
import json
import requests
url = 'https://inputtools.google.com/request?itc=ja-t-i0-handwrit&app=demopage'
data = '{"app_version":0.4,"api_level":"537.36","device":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36","input_type":0,"options":"enable_pre_space","requests":[{"writing_guide":{"writing_area_width":200,"writing_area_height":200},"pre_context":"","max_num_results":1,"max_completions":0,"ink":[[[100,100],[20,180],[0,1]],[[20,180],[100,100],[2,3]]]}]}'
headers = {'content-type': 'application/json'}
r = requests.post(url, json=data, headers=headers)
print r.json()
加长版: 我有此Javascript,成功地使一个Ajax调用。我向控制台打印响应,并可以从我发送的输入中看到一系列建议字符。
var text = {
'app_version' : 0.4,
'api_level' : '537.36',
'device' : window.navigator.userAgent,
'input_type' : 0, // ?
'options' : 'enable_pre_space', // ?
'requests' : [ {
'writing_guide' : {
'writing_area_width' : 200, // canvas width
'writing_area_height' : 200, // canvas height
},
'pre_context' : '', // confirmed preceding chars
'max_num_results' : 1,
'max_completions' : 0,
'ink' : []
} ]
};
// written coordinates to be sent
text.requests[0].ink = [
[[100,100],[20,180],[0,1]],
[[20,180],[100,100],[2,3]],
];
console.log(JSON.stringify(text))
$.ajax({
url : 'https://inputtools.google.com/request?itc=ja-t-i0-handwrit&app=demopage',
method : 'POST',
contentType : 'application/json',
data : JSON.stringify(text),
dataType : 'json',
}).done(function(json) {
console.log(json);
});
输出:
[ “SUCCESS”,[[ “fb02254b519a9da2”,[ “+”, “十”, “T”, “T”,“ナ”, “F”, “子”, “干”, “1”, “千”],[],[目标对象] {is_html_escaped:假}]]]
现在我正在努力在Python中复制。我试过使用上面的代码和它的许多变体,但每次我收到响应'FAILED_TO_PARSE_REQUEST_BODY'。 Ajax和Python调用之间有什么不同,导致我的请求失败?
这个问题类似于this和this,但它们涉及多次使用相同的密钥和不正确的数据编码,我不认为这适用于这种情况。