Google Web Search API会在不同的搜索中返回相同查询字符串的随机结果

时间:2016-02-19 13:01:49

标签: java json gson google-search-api

大家好我正在开展项目,我正在使用Google Web Search API进行网络搜索。它以随机方式工作,它为不同搜索的相同查询字符串产生不同的结果集。会有什么问题。 请尽可能尽快回复。我正在过滤结果以获取特定网站的建议。 这是我的WebSearch代码。 “

public Map getSearchArray(String search_key) throws Exception
{
        Reader reader = null;
        GoogleResults results = null;
        URL url = null;
        int index=0;
        int result_size=0;
        Map search_result_map = new HashMap();

        try {
            logger.info("Inside GetSearch");
            for (int i = 0; i < 20 ; i=i+4) {
                String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+i+"&q=";
                String charset = "UTF-8";
                url = new URL(google + URLEncoder.encode(search_key, charset));
                reader = new InputStreamReader(url.openStream(), charset);
                results = new Gson().fromJson(reader, GoogleResults.class);
                //logger.info("resultSize: "+results.getResponseData().getResults().size());
                //System.out.println("total"+t);
                if (results.getResponseData().getResults().size()!=0) {
                    result_size = results.getResponseData().getResults().size();
                } else {
                    result_size = 1;
                }

                for (int m = 0; m <result_size; m++)
                {
                    String a=results.getResponseData().getResults().get(m).getUrl();
                    System.out.println(a);
                    if(a.matches(".*food.Some-Website.*")
                                ||a.matches(".*Some-Website.*")
                                ||a.matches(".*Some-Website.*")
                                ||a.matches(".*Some-Website.*")
                    {
                        logger.info("----I----"+i+"----M----"+m);
                        String title = results.getResponseData().getResults().get(m).getTitle().replaceAll("\\<[^>]*>","");
                        String url_link = results.getResponseData().getResults().get(m).getUrl();
                        String embeded_link = createEmbeddedLink(url_link, title, "http");
                        search_result_map.put("link_"+index, embeded_link);
                        search_result_map.put("title_"+index, title);
                        index++;
                        logger.info("Title "+title);
                    }
                    else
                    {
                        System.out.println("No Url");
                    }
                    }
                }
            } catch (Exception e)
            {
                logger.info("Exception Thrown");
            }
        finally {
            return search_result_map;
        }
    }

`

0 个答案:

没有答案