用Java搜索谷歌

时间:2012-11-14 06:33:46

标签: google-search-api

这是我在StackExchange上找到的一些代码:

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

String key="YOUR KEY";
String qry="Android";
URL url = new URL(
        "https://www.googleapis.com/customsearch/v1?key="+key+ "&cx=013036536707430787589:_pqjad5hr1a&q="+ qry + "&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {

    if(output.contains("\"link\": \"")){                
        String link=output.substring(output.indexOf("\"link\": \"")+("\"link\": \"").length(), output.indexOf("\","));
        System.out.println(link);       //Will print the google search links
    }     
}
conn.disconnect();                              
}

我的问题是:如何使用filetype限制搜索:pdf?

1 个答案:

答案 0 :(得分:0)

使用搜索项目的:<file-extension in the query>

例如 GET https://www.googleapis.com/customsearch/v1? 键= INSERT-YOUR-KEY&安培; CX = 013036536707430787589:_pqjad5hr1a&安培; Q = flowers:pdf&安培; ALT = JSON

适用于其他参数访问 https://developers.google.com/custom-search/v1/using_rest