Google新闻Api自定义搜索

时间:2013-11-15 19:40:26

标签: javascript google-api google-news

我在我的html文件中有这个java脚本....我在我的网页上有一个关键字搜索,我想将关键字搜索传递给API。这就是我目前所拥有的:不知道我在哪里出错,它没有产生任何搜索结果。

我在哪里

CNN

在代码中,这是我的搜索变量应该传递的地方......

<script type="text/javascript">

    google.load('search', '1');

    function OnLoad()
    {
        var customSearchControl = new google.search.CustomSearchControl('CNN');
        customSearchControl.setResultSetSize(google.search.Search.FILTERED_
                                                             CSE_RESULTSET);
        customSearchControl.draw('cse');
        customSearchControl.execute("$q");
    }
    google.setOnLoadCallback(OnLoad);

     // tell the searcher to draw itself and tell it where to attach
      // create a drawOptions object
      var drawOptions = new google.search.DrawOptions();

      // tell the searcher to draw itself in tabbed mode
      drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
      searchControl.draw(document.getElementById("content"),drawOptions);

      searchControl.addSearcher(new google.search.NewsSearch());
    }

    google.setOnLoadCallback(OnLoad);

    </script>

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我认为问题是这些专业的CSE自2010年以来一直被“弃用”(见http://code.google.com/apis/websearch/),现在它们只是不起作用。新的工作是一般的网络搜索和图像搜索。如果这是您的目标,您可以将网络搜索限制为CNN网站。见What are the alternatives now that the Google web search API has been deprecated?

答案 1 :(得分:0)

您可以使用像这样的第三方解决方案:https://serpapi.com/news-results

它的工作方式与Google常规搜索非常相似,只需将tbm=nws传递到您的网址即可。

https://serpapi.com/search.json?q=Trump&tbm=nws&location=Dallas&hl=en&gl=us

结果:

{
  ...
  "news_results": [
    {
      "position": 1,
      "title": "Trump blasts Sessions over indictments of two of his earliest ...",
      "link": "https://www.cnn.com/2018/09/03/politics/donald-trump-jeff-sessions-justice-department/index.html",
      "source": "CNN",
      "date": "4 hours ago",
      "snippet": "(CNN) President Donald Trump on Monday blasted his Attorney General Jeff Sessions and lamented the indictments of two lawmakers who ...",
      "thumbnail": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSCv3eR2GkOR3WgDNTd4LqT77Bteu_ScA8NUFKcEYU1HskYXHXrnse-GnG0SN78_uUgM1k0nIQV"
    },
    {
      "position": 2,
      "title": "Trump accuses Sessions of hurting Republican congressional races",
      "link": "https://www.reuters.com/article/us-usa-trump-sessions/trump-accuses-sessions-of-hurting-republican-congressional-races-idUSKCN1LJ231",
      "source": "Reuters",
      "date": "3 hours ago"
    },
    {
      "position": 3,
      "title": "Trump steams at Attorney General Jeff Sessions, reigniting his attacks",
      "link": "https://abcnews.go.com/Politics/trump-steams-attorney-general-jeff-sessions-reigniting-attacks/story?id=57579418",
      "source": "ABC News",
      "date": "3 hours ago"
    },
    {
      "position": 4,
      "title": "Trump slams Sessions on Twitter, says AG is hurting GOP in midterms",
      "link": "http://www.foxnews.com/politics/2018/09/03/trump-slams-sessions-on-twitter-says-ag-is-hurting-gop-in-midterms.html",
      "source": "Fox News",
      "date": "4 hours ago"
    },
    {
      "position": 5,
      "title": "Trump blasts Sessions over charges against GOP congressmen ...",
      "link": "https://www.politico.com/story/2018/09/03/donald-trump-jeff-sessions-tweet-806298",
      "source": "Politico",
      "date": "5 hours ago"
    },
    ...
  ],
  ...
}