Google custom search api表示要使用参数num
来指定图像结果的数量,这仅从1到10。我一直在寻找如何重复搜索以获得超过10个图像和I found this article。他说:
即使我们可以从服务器获得8张图片的限制 在单个查询中,如果我们最终修改
start parameter
检索超过8个结果。
从上面链接的文档中,start
表示 要返回的第一个结果的索引。
因此,我已应用他用于运行查询的内容,并将其应用于我自己的代码,但现在我得到0张图像。
var myCx = "MY_CX";
var myKey = "MY_KEY";
var lastQueriedName;
var termS = "hello";
// This bit is from the link which runs against the currentSTARTposition
var currentSearchPosition = 0;
function calculateStartPosition(termS, shift) {
if (lastQueriedName === termS) {
currentSearchPosition += shift;
if (currentSearchPosition < 0) {
currentSearchPosition = 0;
}
if (currentSearchPosition >= 100) {
currentSearchPosition = 92;
}
} else {
lastQueriedName = termS;
currentSearchPosition = 0;
}
return currentSearchPosition;
}
// This is my own code which works only if
// I don't insert the above function and I remove start: position
$.getJSON("https://www.googleapis.com/customsearch/v1", {
q: termS,
alt: "json",
searchType: "image",
cx: myCx,
start: currentSearchPosition,
num: 10,
key: myKey,
rights: "cc_publicdomain",
filter: "1",
imgType: "photo",
fileType: "jpg"
},
function (data) {
$.each(data.items, function(i,item){
$(".my_images .row").append('<div class="col-sm-4"><div class="thumbnail"><img class="img-responsive" src="' + item.link + '"></div></div>');
});
});
};
答案 0 :(得分:0)
最终这就是我抓取超过10张图片的方式,我重复查询并使用start参数:我在调用之间保存起始值
function createGoogleImagesLoader(initialValue) {
var _start = initialValue || 1;
var imagesCount = 10;
return function() {
$.getJSON("https://www.googleapis.com/customsearch/v1", {
...
num: imagesCount,
start: _start
},..);
_start += imagesCount;
}
}
var googleImages = createGoogleImagesLoader();
googleImages() // load from 0 to 10
googleImages() // load from 10 to 20 etc.