【问题标题】:how to extract the number of google hits for many term continuously?如何连续提取多个术语的谷歌点击次数?
【发布时间】:2013-04-02 16:45:46
【问题描述】:

我想用HttpClient的类来连续提取多个词条的google点击次数,但是google服务器不让我重复做这个操作,你能帮帮我吗?这是我的程序,参数Concept是我要搜索的词。

public static double extractGoogleCount(String Concept)
    {
    double temp = 0;
    HttpClient httpClient = new HttpClient();
    String url = "http://www.google.com/search?hl=en&newwindow=1&q="
        + Concept + "&aq=f&aqi=&aql=&oq=&gs_rfai=";
    GetMethod getMethod = new GetMethod(url);
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
        new DefaultHttpMethodRetryHandler());
    try
    {
        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK)
        {
            System.err.println("Method failed: "
                + getMethod.getStatusLine() + url);
        }
        InputStream responseBody = getMethod.getResponseBodyAsStream();
        DataInputStream dis = new DataInputStream(responseBody);
        String returnPage = dis.readLine();
        while (returnPage != null)
        {
            int index = returnPage.indexOf("<div id=\"resultStats\">");
            if (index == -1)
            {
            returnPage = dis.readLine();
            continue;
            }
            String sub = returnPage.substring(index, index + 100);
            if (sub.indexOf("About") >= 0)
            {

            String[] result = sub.split(" ");
            String number = result[2].replaceAll(",", "");
            temp = Double.parseDouble(number);
            } else
            {
            String[] result = sub.split(" ");
            String number = result[1].substring(result[1]
                .indexOf(">") + 1);
            System.out.println("number:" + number);
            temp = Double.parseDouble(number);
            }
            break;
        }

        return temp;
    } catch (HttpException e)
    {
        System.out.println("Please check your provided http address!");
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    } 
    catch (Exception e)
    {
        e.printStackTrace();
        return temp;
    } finally
    {
        httpClient.getState().clear();
        getMethod.releaseConnection();

    }

    }

【问题讨论】:

  • 哇!耽误。您能否正确缩进代码,以便它显示为代码。眼睛疼

标签: java httpclient


【解决方案1】:

Google 仅允许每秒来自单个客户端的特定数量的请求。 尝试添加:

 Thread.sleep(200);

到代码,它应该工作。如果您需要以某种方式显示这些数据,您可能需要创建另一个线程来完成获取工作,以便您可以在程序中执行其他操作

【讨论】:

  • 很高兴您回答我的问题。但是,我很抱歉,因为“Thread.sleep(200)”在术语数量很大时不起作用(我想谷歌大约数千个术语)。所以,我想知道我是否可以使用我的程序中的代理?
  • 首先,您使用的是哪个 API?
  • 我用的是 HttpClient ,这里是它的介绍:hc.apache.org/httpclient-3.x/tutorial.html
猜你喜欢
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2018-11-19
  • 2022-10-13
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多