来自Bing API的搜索结果数量

时间:2014-06-17 11:36:50

标签: java bing-api

如何在bing搜索中访问搜索结果的数量? 我发现这个帖子: How to get number of search result from Bing API

在这个帖子中,我明白我应该使用d->results[0]->WebTotal 但我如何在java中使用这一行?

  public static void main(String[] args) throws Exception {

//term1
    String searchText = "is";
    searchText = searchText.replaceAll(" ", "%20");
    String accountKey="WOCKN8uXArczOkQq5phtoEc7usB0kDoPTnbqn0sKWeg";

    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    URL url;
    try {
        url = new URL(
                "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=%27" + searchText + "%27&$top=50&$format=Atom");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
    //conn.setRequestProperty("Accept", "application/json");
    BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
    StringBuilder sb = new StringBuilder();
    String output;
    System.out.println("Output from Server .... \n");
    char[] buffer = new char[4096];
    while ((output = br.readLine()) != null) {
        sb.append(output);
             //text.append(link + "\n\n\n");//Will print the google search links
        //}    
    }

    conn.disconnect();

    int find = sb.indexOf("<d:Description");
    int total = find + 1000;
    System.out.println("Find index: " + find);
    System.out.println("Total index: " + total);
    sb.getChars(find+35, total, buffer, 0);
    String str = new String(buffer);


    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

http://learn-it-stuff.blogspot.com/2012/09/using-bing-custom-search-inside-your.html

1 个答案:

答案 0 :(得分:1)

我找到了答案:

 public static void main(String[] args) {
       String searchText = "searchtext";
       searchText = searchText.replaceAll(" ", "%20");
       String accountKey="key_ID";

    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    URL url;
    try {
        url = new URL(
                "https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27Web%27&Query=%27" + searchText + "%27&$format=JSON");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Authorization", "Basic " + accountKeyEnc);

    BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
    StringBuilder sb = new StringBuilder();
    String output;
    System.out.println("Output from Server .... \n");
    //write json to string sb
    while ((output = br.readLine()) != null) {

        sb.append(output);

    }

    conn.disconnect();
     //find webtotal among output      
   int find= sb.indexOf("\"WebTotal\":\"");
   int startindex = find + 12;


   int lastindex = sb.indexOf("\",\"WebOffset\"");

    System.out.println(sb.substring(startindex,lastindex));

    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


}