2014-04-01 77 views
5

好的我有一个简单的任务,我只想使用Google的搜索引擎,更具体地说,是自动更正部分。让我们说我谷歌这个:https://www.google.com/search?q=matew+mccaonaghey 正如你所看到的,谷歌显示“matthew mcconaughey”的结果,从而autocorrected输入。实施Google建议

所以,我做了一些研究,发现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(); 

回答

0

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

看看这个Google Suggest Scraper,这是一个免费/开源的PHP项目,可以刮掉内部的建议/自动完成API,你使用的和新的。

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

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       | 

希望有所帮助。

+0

谢谢你的解释!然而,我怀疑谷歌会允许刮掉他们的数据,因为这个提议的PHP项目似乎这样做。我想如果会有太多的请求,某些类型的验证码会在谷歌方面激活。 –

+1

为了克服这些限制,该项目使用从IP提供商获取的多个IP,并以防止长期阻止的方式对其进行管理。 我用它得到了数万个结果,没有任何问题(到目前为止)。 是的,如果你只是盲目地查询他们的API,你会被封锁。但在这种情况下,不能通过验证码,你只是没有得到任何答复。 – John