那么您可以做的就是以编程方式执行实际的 Google 搜索。最简单的方法是访问 URL https://www.google.com/search?q=QUERY_HERE,然后您想从该页面中删除结果计数。
这是一个如何做到这一点的快速示例:
private static int getResultsCount(final String query) throws IOException {
final URL url = new URL("https://www.google.com/search?q=" + URLEncoder.encode(query, "UTF-8"));
final URLConnection connection = url.openConnection();
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8");
while(reader.hasNextLine()){
final String line = reader.nextLine();
if(!line.contains("<div id=\"resultStats\">"))
continue;
try{
return Integer.parseInt(line.split("<div id=\"resultStats\">")[1].split("<")[0].replaceAll("[^\\d]", ""));
}finally{
reader.close();
}
}
reader.close();
return 0;
}
对于使用,您可以执行以下操作:
final int count = getResultsCount("horses");
System.out.println("Estimated number of results for horses: " + count);