gpt4 book ai didi

java - 以编程方式获取谷歌搜索结果计数的最简单(合法)方法?

转载 作者:搜寻专家 更新时间:2023-10-31 20:03:24 27 4
gpt4 key购买 nike

我想使用 Java 代码获取某些 Google 搜索引擎查询(在整个网络上)的估计结果数。

我每天只需要做很少的查询,所以一开始 Google Web Search API ,虽然已被弃用,但似乎已经足够好了(参见例如 How can you search Google Programmatically Java API )。但事实证明,此 API 返回的数字与 www.google.com 返回的数字有很大不同(参见例如 http://code.google.com/p/google-ajax-apis/issues/detail?id=32)。所以这些数字对我来说毫无用处。

我也试过Google Custom Search engine ,它表现出同样的问题。

您认为对我的任务来说最简单的解决方案是什么?

最佳答案

/**** @author RAJESH Kharche */
//open Netbeans
//Choose Java->prject
//name it GoogleSearchAPP

package googlesearchapp;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GoogleSearchAPP {
public static void main(String[] args) {
try {
// TODO code application logic here

final int Result;

Scanner s1=new Scanner(System.in);
String Str;
System.out.println("Enter Query to search: ");//get the query to search
Str=s1.next();
Result=getResultsCount(Str);

System.out.println("Results:"+ Result);
} catch (IOException ex) {
Logger.getLogger(GoogleSearchAPP.class.getName()).log(Level.SEVERE, null, ex);
}
}

private static int getResultsCount(final String query) throws IOException {
final URL 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", "Google Chrome/36");//put the browser name/version

final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8"); //scanning a buffer from object returned by http request

while(reader.hasNextLine()){ //for each line in buffer
final String line = reader.nextLine();

if(!line.contains("\"resultStats\">"))//line by line scanning for "resultstats" field because we want to extract number after it
continue;

try{
return Integer.parseInt(line.split("\"resultStats\">")[1].split("<")[0].replaceAll("[^\\d]", ""));//finally extract the number convert from string to integer
}finally{
reader.close();
}
}
reader.close();
return 0;
}
}

关于java - 以编程方式获取谷歌搜索结果计数的最简单(合法)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18204814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com