2016-05-22 44 views
0

自从谷歌从其更传统的搜索引擎api转向支持谷歌自定义搜索API后,自定义搜索引擎已经有所失落。我希望有人能够告诉我一个(非常简单)的目标是否可以用新框架来完成,并且可能会有任何帮助。谷歌自定义搜索API - 搜索结果

具体来说,我正在寻找一个程序,它将从文本文件中读取文本,然后在谷歌搜索中使用所述文档中的五个单词 - 重点是找出从所述搜索中产生的结果。

一例输入/输出将是:

输入:“这是我的搜索术语” - 包括在搜索中的报价!

输出:有7个总成绩

感谢这么多,总之,你的时间/帮助

回答

1

首先,你需要创建一个谷歌自定义搜索项目中你的Google帐户。 在这个项目中,您必须获得一个自定义搜索引擎ID,称为cx参数。您还必须获取API密钥参数。这两个都可以从您的Google帐户中的Google Custom Search API项目中获得。

然后,如果你喜欢Java的,这里有一个工作示例:

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 


public class GoogleCustonSearchAPI { 

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

String key="your_key"; 
String qry="your_query"; 
String cx = "your_cx"; 

//Fetch urls 
URL url = new URL(
"https://www.googleapis.com/customsearch/v1?key="+key+"&cx="+cx+"&q="+ qry +"&alt=json&queriefields=queries(request(totalResults))"); 

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestMethod("GET"); 
conn.setRequestProperty("Accept", "application/json"); 
BufferedReader br = new BufferedReader(new InputStreamReader(
     (conn.getInputStream()))); 
//Remove comments if you need to output in JSON format 
/*String output; 
System.out.println("Output from Server .... \n"); 
while ((output = br.readLine()) != null) { 
    System.out.println(output); 
}*/ 
//Print the urls and domains from Google Custom Search                    String searchResult;   
    while ((searchResult = output.readLine()) != null) { 
     int startPos=searchResult.indexOf("\"link\": \"")+("\"link\": \"").length(); 
     int endPos=searchResult.indexOf("\","); 
     if(searchResult.contains("\"link\": \"") && (endPos>startPos)){ 
      String link=searchResult.substring(startPos,endPos); 
      if(link.contains(",")){ 
       String tempLink = "\""; 
       tempLink+=link; 
       tempLink+="\""; 
       System.out.println(tempLink); 
      } 
      else{ 
       System.out.println(link);     
      } 
      System.out.println(getDomainName(link)); 
     }  
    } 
conn.disconnect();     
} 

public static String getDomainName(String url) throws URISyntaxException { 
    URI uri = new URI(url); 
    String domain = uri.getHost(); 
    return domain.startsWith("www.") ? domain.substring(4) : domain; 
} 

的 “& queriefields =查询(请求(使用totalResults))” 是什么使差异,并给出SOU你需要什么。但请记住,您每天只能免费执行100个查询,而且自定义搜索API的结果有时与Google.com搜索返回的结果有很大不同。

+0

你介意如何 “特别” 获得'cx'? – lonesome

+0

在这里您可以找到cx id:http://www.google.com/cse/manage/all。然后查找“我的搜索引擎”,然后从列表中选择。当你选择你的引擎时,右边有一个按钮“搜索引擎ID”。这是cx。希望我帮助! –

+0

这很有帮助。但我有一个问题。我想知道Google为任何随机查询返回的结果。由于谷歌杀死了Ajax api,我正在尝试使用CSE。但我不知道如何使用它。我选择Google.com来创建自定义搜索引擎,但它会返回Google服务的结果。我应该如何制作一个自定义的搜索引擎来显示Google的搜索结果? – lonesome

0

如果有人仍然需要一些CSE示例(谷歌自定义搜索引擎)API,这是工作方法

public static List<Result> search(String keyword){ 
    Customsearch customsearch= null; 


    try { 
     customsearch = new Customsearch(new NetHttpTransport(),new JacksonFactory(), new HttpRequestInitializer() { 
      public void initialize(HttpRequest httpRequest) { 
       try { 
        // set connect and read timeouts 
        httpRequest.setConnectTimeout(HTTP_REQUEST_TIMEOUT); 
        httpRequest.setReadTimeout(HTTP_REQUEST_TIMEOUT); 

       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    List<Result> resultList=null; 
    try { 
     Customsearch.Cse.List list=customsearch.cse().list(keyword); 
     list.setKey(GOOGLE_API_KEY); 
     list.setCx(SEARCH_ENGINE_ID); 
     Search results=list.execute(); 
     resultList=results.getItems(); 
    } 
    catch ( Exception e) { 
     e.printStackTrace(); 
    } 
    return resultList; 
} 

这个方法返回结果对象的列表,所以你可以通过它

List<Result> results = new ArrayList<>(); 

    try { 
     results = search(QUERY); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    for(Result result : results){ 
     System.out.println(result.getDisplayLink()); 
     System.out.println(result.getTitle()); 
     // all attributes 
     System.out.println(result.toString()); 
    } 

我使用gradle这个依赖

迭代
dependencies { 
compile 'com.google.apis:google-api-services-customsearch:v1-rev57-1.23.0' 
} 

不要忘记定义自己GOOGLE_API_KEY,SEARCH_ENGINE_ID(CX),查询和HTTP_REQUEST_TIMEOUT(即私有静态最终诠释HTTP_REQUEST_TIMEOUT = 3 * 60万;)