Google自定义搜索API-搜索结果 [英] Google Custom Search API - Search Results

查看:104
本文介绍了Google自定义搜索API-搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从Google从较旧的搜索引擎API转向Google自定义搜索API以来,我就一直与自定义搜索引擎失去联系.我希望有人能够告诉我,使用新框架是否可以实现一个(非常简单的)目标,并且可能会有任何起步帮助.

I have somewhat lost touch with custom search engines ever since Google switched from its more legacy search engine api in favor of the google custom search api. I'm hoping someone might be able to tell me whether a (pretty simple) goal can be accomplished with the new framework, and potentially any starting help would be great.

具体来说,我正在寻找一个程序,该程序将从文本文件中读取文本,然后在Google搜索中使用来自所述文档的五个单词-重点是要弄清楚从该搜索中获得多少结果.

Specifically, I am looking to write a program which will read in text from a text file, then use five words from said document in a google search - the point being to figure out how many results accrue from said search.

示例输入/输出为:

输入:这是我的搜索字词"-包含在搜索中的报价!

Input: "This is my search term" -- quotations included in the search!

输出:总共有7个结果

非常感谢您的时间/帮助

Thanks so much, all, for your time/help

推荐答案

首先,您需要在自己的google帐户中创建一个Google自定义搜索项目. 从此项目中,您必须获得一个自定义搜索引擎ID,称为cx参数.您还必须获取API密钥参数.两者都可以从您的Google帐户中的Google自定义搜索API项目中获得.

First you need to create a Google Custom Search project inside you google account. From this project you must obtain a Custom Search Engine ID , known as cx parameter. You must also obtain a API key parameter. Both of these are available from your Google Custom Search API project inside your google account.

然后,如果您喜欢Java,这是一个有效的示例:

Then, if you prefer Java , here's a working example:

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 = queries(request(totalResults))"的作用与众不同,可以满足您的需求.但是请记住,您每天只能免费执行100个查询,并且Custom Search API的结果有时与Google.com搜索返回的结果大不相同

The "&queriefields=queries(request(totalResults))" is what makes the difference and gives sou what you need. But keep in mind that you can perform only 100 queries per day for free and that the results of Custom Search API are sometimes quite different from the those returned from Google.com search

这篇关于Google自定义搜索API-搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆