【发布时间】:2016-03-07 17:54:22
【问题描述】:
我正在使用 Google 自定义搜索 API,它为我提供了 json 格式的搜索结果。 (Docs)
我需要获取节点og:description 的内容,但显然它并不像我想象的那么简单。我试过使用这个:items.pagemap.metatags[0].og:description 这不起作用 - 分号会导致错误。
我的其余代码看起来像这样并且正在运行:
var key = "xxx";
var cx = "xxx";
var q = "cars";
var url = "https://www.googleapis.com/customsearch/v1?q=" + encodeURIComponent(q) + " &prettyPrint=false&cx=" + cx + "&key=" + key + "";
$.getJSON(url, function(data) {
var news = [];
if (data.items) {
$.each(data.items, function(key, i) {
news.push("<li><img src='" + i.pagemap.cse_image[0].src + "'><a target='_blank' href='" + i.link + "'>" + i.title + "</a><div class='description'>" + i.snippet + "</div></li>");
//console.log(data);
});
}
$("<ul/>", {
html: news.join("")
}).appendTo("#content_0");
}).fail(function() {
console.log("error");
});
非常感谢任何帮助。
【问题讨论】:
标签: jquery json google-api google-search-api