Javascript 谷歌新闻Api自定义搜索
我的html文件中有这个java脚本……我的网页中有一个关键字搜索,我想将关键字搜索传递给API。这就是我目前所拥有的:我不确定哪里出了问题,因为它没有生成任何搜索结果 我在哪里Javascript 谷歌新闻Api自定义搜索,javascript,google-api,google-news,Javascript,Google Api,Google News,我的html文件中有这个java脚本……我的网页中有一个关键字搜索,我想将关键字搜索传递给API。这就是我目前所拥有的:我不确定哪里出了问题,因为它没有生成任何搜索结果 我在哪里 CNN 在代码中,这是我的搜索变量应该被传递的地方 <script type="text/javascript"> google.load('search', '1'); function OnLoad() { var customSearchControl =
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>
load('search','1');
函数OnLoad()
{
var customSearchControl=new google.search.customSearchControl('CNN');
customSearchControl.setResultSetSize(google.search.search.FILTERED_
CSE_结果集);
customSearchControl.draw('cse');
customSearchControl.execute(“$q”);
}
setOnLoadCallback(OnLoad);
//告诉搜索者自己画出来,并告诉它连接到哪里
//创建drawOptions对象
var drawOptions=new google.search.drawOptions();
//告诉搜索者以选项卡模式绘制自己
setDrawMode(google.search.SearchControl.DRAW_模式选项卡);
draw(document.getElementById(“内容”)、drawOptions;
searchControl.addSearcher(新的google.search.NewsSearch());
}
setOnLoadCallback(OnLoad);
我也有同样的问题,我认为问题在于这些专门的CSE自2010年以来就被“弃用”了(请参阅),现在它们根本不起作用。新的搜索引擎适用于一般的网络搜索和图像搜索。如果这是你的目标,你可以把网络搜索限制在CNN网站上。请参见您可以使用像这样的第三方解决方案:
它的工作方式与Google常规搜索非常相似,只需将tbm=nws
传递到您的url即可
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"
},
...
],
...
}