在python中向Google输入工具发送请求

时间:2017-03-13 18:23:15

标签: python google-api google-input-tools

我想向Google Input API发送一些(约200k)请求并显示结果。例如,我想发送deep,它必须返回:

["deep","dépenses","dépend","dépendance","dépendant"]

我尝试过以下脚本,但没有帮助:

def request(word):
    url = 'https://inputtools.google.com/request?text=' + word + '&itc=fr-t-i0-und&num=5&cp=0&cs=1&ie=utf-8&oe=utf-8&app=test'
    conn = http.client.HTTPConnection(url)
    res = conn.getresponse()
    print(res.status, res.reason)

我想知道如何实现这一目标? (参见我想要实现的例子here

1 个答案:

答案 0 :(得分:2)

我不确定你的调用语法在哪里,但是如果你read the documentation,你会发现你需要把代码写成

import http.client
def request(word):
    conn = http.client.HTTPSConnection('inputtools.google.com')
    conn.request('GET', '/request?text=' + word + '&itc=fr-t-i0-und&num=5&cp=0&cs=1&ie=utf-8&oe=utf-8&app=test')
    res = conn.getresponse()
    print(res.status, res.reason)
    print(res.read())

request(word='deep')