谷歌新闻 API 自定义搜索



我的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>

您可以使用如下所示的第三方解决方案: 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"
    },
    ...
  ],
  ...
}

我有同样的问题,我认为问题是这些专门的 CSE 自 2010 年以来已被"弃用"(见 http://code.google.com/apis/websearch/),现在它们只是不起作用。新的工作是用于一般的网络搜索和图像搜索。如果这是您的目标,您可能会将网络搜索限制在CNN网站上。请参阅现在 Google 网页搜索 API 已被弃用,有哪些替代方案?

相关内容

  • 没有找到相关文章

最新更新