【问题标题】:Google Ajax Search API no results from site:mywebsite.comGoogle Ajax 搜索 API 没有来自 site:mywebsite.com 的结果
【发布时间】:2014-03-27 14:16:23
【问题描述】:

我正在为我的网站使用 Google Search API。 除了一个之外,所有搜索查询一切正常! 包含site:noviniran.com 的查询不返回任何结果!!!! 我不知道为什么!此查询在普通 Google.com 上有效,但在 Google Search API 上不返回任何内容。

退房:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:noviniran.com

其他查询返回没有问题的结果:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:iran.com

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:iranair.com

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:irannovin.com

我 1 岁的网站怎么了? :(

抱歉英语不好,

提前谢谢你

【问题讨论】:

  • 没有人能帮我解决这个问题???
  • 该 API 已被弃用,所以我猜测它使用的搜索索引与公共 Google 搜索不同。新的搜索 API 位于 developers.google.com/custom-search/docs/overview
  • 我知道它已被弃用,但为什么它适用于其他查询?
  • 可能是因为这些网站较旧?我尝试使用我拥有的网站,返回的结果来自 2009 年

标签: javascript jquery ajax json google-api


【解决方案1】:

当我现在对此进行测试时,它以The Google Web Search API is no longer available. Please migrate to the Google Custom Search API 响应。所以我会尽力举例说明custom search api是如何工作的。

function getData(query, engine, search_key) {
	/*
		Info about google custom search API:
	
		The first 100 queries per day are free. 
		Any more, then you have to pay $5 per 1000 queries, for up to 10,000 queries per day, 
		just enable billing to do so. Each query returns a maximum of 10 results, 
		so you can retrieve 1000 URL’s from your search per day for free.
	*/
	
	var API_KEY = search_key;
	var ENGINE_ID = engine;
	var API_URL = `https://www.googleapis.com/customsearch/v1?key=${API_KEY}&cx=${ENGINE_ID}&searchType=image&q=${query}`;
  
  jQuery.getJSON(API_URL, {
            tags: query,
            tagmode: "any",
            format: "json"
        },
        function(data) {
            //do something
        });
}
<div id="image" onload="getData("your_search", "your_engine_id", "your_key")"></div>

但是在运行这个 sn-p 之前,首先在 Google 上为此创建一个 API key 和一个 custom search engine,您将需要它是 ID。此外,在创建 custom engine 后,您需要启用 Image search 选项。

API console

Search engine

Custom search API key

当您创建它时,您需要确保您有一个字符串构建,其中请求api key 和search engine ID,如下所示:

https://www.googleapis.com/customsearch/v1?key=${API_KEY}&cx=${ENGINE_ID}&searchType=image&q=${query}

第二部分是您需要将字符串设置为getJSON,因为您想要来自该搜索的数据,如下所示:

jQuery.getJSON(API_URL, {
            tags: query,
            tagmode: "any",
            format: "json"
        },

最后你想要一个函数来关闭json,这样你就可以像这样对数据做任何你想做的事情:

function(data) {
    //do whatever
});

请记住,这只是一个示例,可帮助您进一步推进项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多