如何计算查询中的当前索引位置?
How to calculate current index position in a query?
Google custom search api says to use the parameter num
to specify the number of image results, this goes from 1 to 10 only. I have been looking around how to sort of repeat the search to get more than 10 images and I found this article。他说:
Even though there is a limit of 8 pictures we can get from the server
in a single query, if we modify the start parameter
we can ultimately
retrieve more than 8 results.
从上面链接的文档中,start
表示 第一个结果的索引 return。
因此,我将他用于 运行 查询的内容应用到我自己的代码中,但我现在得到 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>');
});
});
};
最终这就是我抓取 10 多张图像的方式,我重复查询并使用开始参数:我在调用之间保存开始值
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.
Google custom search api says to use the parameter num
to specify the number of image results, this goes from 1 to 10 only. I have been looking around how to sort of repeat the search to get more than 10 images and I found this article。他说:
Even though there is a limit of 8 pictures we can get from the server in a single query, if we modify the
start parameter
we can ultimately retrieve more than 8 results.
从上面链接的文档中,start
表示 第一个结果的索引 return。
因此,我将他用于 运行 查询的内容应用到我自己的代码中,但我现在得到 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>');
});
});
};
最终这就是我抓取 10 多张图像的方式,我重复查询并使用开始参数:我在调用之间保存开始值
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.