实施Google建议

Non*_*one 5 java search-engine

好的我有一个简单的任务,我只想使用谷歌的搜索引擎,更具体地说,是自动更正部分.让我说我谷歌这个:https://www.google.com/search? q =matew + mccaonaghey正如你所看到的,谷歌显示了"matthew mcconaughey"的结果,因此自动修正了输入.

所以,我做了一些研究,发现http://suggestqueries.google.com可用于查询此类输入.虽然它大多数时候都运行正常,但最有趣的是:当我试图获得"matew mccaonaghey"的结果时,我得到了一个空的JSON.如果我将搜索字符串更改为"mathew mccoanaghey",结果就可以了.

我错过了什么?不建议queries.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();
Run Code Online (Sandbox Code Playgroud)

Joh*_*ohn 0

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

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

我在两种模式下启动它以确保我是对的,结果如下:
Firefox 模式:

Google Suggest Spider results
Recursion level 0 contains 0 keywords:
| Keyword                                            | Suggests                                           |
| -------------------------------------------------- | -------------------------------------------------- |
Run Code Online (Sandbox Code Playgroud)

新模式:

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                         |
Run Code Online (Sandbox Code Playgroud)

希望有帮助。