以编程方式获取谷歌搜索结果计数的最简单(合法)方式? [英] easiest (legal) way to programmatically get the google search result count?

查看:141
本文介绍了以编程方式获取谷歌搜索结果计数的最简单(合法)方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the estimated result count for certain Google search engine queries (on the whole web) using Java code.

我每天只需要做很少的查询,所以最初 Google Web Search API 虽然已被弃用,但看起来还不错(例如如何在Google Programmatically Java API中搜索)。但事实证明,此API返回的数字与www.google.com返回的数字非常不同(请参阅例如 http://code.google.com/p/google-ajax-apis/issues/detail?id=32 )。所以这些数字对我来说都没用。

I need to do only very few queries per day, so at first Google Web Search API, though deprecated, seemed good enough (see e.g. How can you search Google Programmatically Java API). But as it turned out, the numbers returned by this API are very different from those returned by www.google.com (see e.g. http://code.google.com/p/google-ajax-apis/issues/detail?id=32). So these numbers are pretty useless for me.

我也试过 Google自定义搜索引擎,它会出现同样的问题。

I also tried Google Custom Search engine, which exhibits the same problem.

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

What do you think is the simplest solution for my task?

推荐答案

/**** @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;
    }
}

这篇关于以编程方式获取谷歌搜索结果计数的最简单(合法)方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆