【问题标题】:Implementing Google suggestions实施 Google 建议
【发布时间】:2014-04-01 20:36:34
【问题描述】:

好的,我有一个简单的任务,我只想使用 Google 的搜索引擎,更具体地说,自动更正部分。假设我谷歌这个:https://www.google.com/search?q=matew+mccaonaghey 如您所见,Google 显示了“matthew mcconaughey”的结果,因此自动更正了输入。

所以,我做了一些研究,发现http://suggestqueries.google.com 可以用来查询这样的输入。虽然它大部分时间都运行良好,但最有趣的是:当我尝试获取“matew mccaonaghey”的结果时,我得到了一个空的 JSON。如果我将搜索字符串更改为“mathew mccoanaghey”,则结果正常。

我错过了什么? Suggestquerys.google.com 与 www.google.com 的工作方式不同吗?为什么在使用 google.com 时出现建议查询和实际结果时会得到空 json?

感谢您的回答。

代码如下:

URL url = new URL("http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q=matew+mccaonaghey");
URLConnection conn = (URLConnection) url.openConnection();
conn.setRequestProperty("User-Agent",
    "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
conn.connect();
BufferedReader serverResponse = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));
System.out.println(serverResponse.readLine());
serverResponse.close();

【问题讨论】:

    标签: java search-engine


    【解决方案1】:

    原因很简单:)
    Google 正在使用不同的内部 API。 Firefox 建议 API 是一个过时的 API,并且确实会给您一些字符串的空响应。但它也可能导致更多或不同的结果,原因在于 Google 的代码。
    此外,新 API 可以在单个查询中返回两倍的结果。

    查看Google Suggest Scraper,这是一个免费/开源的 PHP 项目,它可以抓取您使用的和较新的两个内部建议/自动完成 API。

    我以两种模式启动它以确保我是正确的,结果如下:
    火狐模式:

    Google Suggest Spider results
    Recursion level 0 contains 0 keywords:
    | Keyword                                            | Suggests                                           |
    | -------------------------------------------------- | -------------------------------------------------- |
    

    新模式:

    Recursion level 0 contains 20 keywords:
    | Keyword                                            | Suggests                                           |
    | -------------------------------------------------- | -------------------------------------------------- |
    | matew mccaonaghey                                  | matthew mcconaughey                                |
    |                                                    | matthew mcconaughey movies                         |
    |                                                    | matthew mcconaughey true detective                 |
    |                                                    | matthew mcconaughey alright alright alright        |
    |                                                    | matthew mcconaughey oscar speech                   |
    |                                                    | matthew mcconaughey diet                           |
    |                                                    | matthew mcconaughey speech                         |
    |                                                    | matthew mcconaughey dallas buyers club             |
    |                                                    | matthew mcconaughey hair                           |
    |                                                    | matthew mcconaughey dazed and confused             |
    |                                                    | matthew mcconaughey woody harrelson                |
    |                                                    | matthew mcconaughey oscar                          |
    |                                                    | matthew mcconaughey weight loss                    |
    |                                                    | matthew mcconaughey height                         |
    |                                                    | matthew mcconaughey workout                        |
    |                                                    | matthew mcconaughey hbo                            |
    |                                                    | matthew mcconaughey wolf of wall street            |
    |                                                    | matthew mcconaughey golden globes                  |
    |                                                    | matthew mcconaughey net worth                      |
    |                                                    | matthew mcconaughey skinny                         |
    

    希望对您有所帮助。

    【讨论】:

    • 感谢您的解释!但是,我怀疑谷歌会允许像这个提议​​的 PHP 项目那样抓取他们的数据。我认为如果有太多请求,某种验证码会在谷歌方面被激活。
    • 为了克服限制,该项目使用从 IP 提供商处获取的多个 IP,并以防止长期阻塞的方式管理它们。我用它获得了数万个结果,没有任何问题(到目前为止)。是的,如果你只是盲目地查询他们的 API,你就会被阻止。但在这种情况下,不是通过验证码,您将不再收到任何回复。
    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多