【发布时间】:2012-07-02 08:20:20
【问题描述】:
以下代码取自:Java code for using google custom search API。它可以正常提取谷歌结果页面中第一页的前 10 个结果。
public static void main(String[] args) throws Exception {
String key="YOUR KEY";
String qry="Android";
URL url = new URL("https://www.googleapis.com/customsearch/v1?
key="+key+ "&cx=013036536707430787589:_pqjad5hr1a&q="+ qry + "&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br =
new BufferedReader(new InputStreamReader( (conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null)
{
if(output.contains("\"link\": \""))
{
String link=output.substring(output.indexOf("\"link\": \"")+
("\"link\": \"").length(), output.indexOf("\","));
System.out.println(link); //Will print the google search links
}
}
conn.disconnect();
}
我想知道如何遍历所有结果页面?通过在https://developers.google.com/custom-search/v1/using_rest 中搜索,我发现查询中的start 参数指的是索引,很明显,通过在循环中更改此值可以达到目的,但会花费我对每个页面的查询(这不应该是这种情况,因为它不是一个新查询,它是同一个查询,但只是新页面)。另外,我发现谷歌已经提到如果查询成功,响应数据包含值totalResults 的总结果,但他们提到这是估计数字。那么,如何获得这项服务的好处并获得实际的结果数或页面数以便遍历它们?我为每个页面发出新查询没有任何意义。
【问题讨论】:
-
@Pargat:请检查这个问题。
标签: java json parsing google-search-api google-custom-search