Javascript 多次调用节点google自定义搜索API
作为参考,我尝试创建一个函数,该函数将单词作为输入,将其切分为32个单词,并返回不同搜索数据的列表: 异步函数singleSearch(q、cx、auth){ const res=wait customsearch.cse.list({ cx:cx, 问:问,, auth:auth, }); 返回res.data; }; 函数搜索(words,apikey=process.argv[2],engineid=process.argv[3]){ const limit=32;//谷歌搜索的字数限制为32 var searchquerys=[]; var len=Math.ceil(单词长度/限制); 对于(变量i=0;iJavascript 多次调用节点google自定义搜索API,javascript,node.js,asynchronous,google-search-api,Javascript,Node.js,Asynchronous,Google Search Api,作为参考,我尝试创建一个函数,该函数将单词作为输入,将其切分为32个单词,并返回不同搜索数据的列表: 异步函数singleSearch(q、cx、auth){ const res=wait customsearch.cse.list({ cx:cx, 问:问,, auth:auth, }); 返回res.data; }; 函数搜索(words,apikey=process.argv[2],engineid=process.argv[3]){ const limit=32;//谷歌搜索的字数限制为
Promise{}
,无论有多少wait/async
或Promise.all()
,如果我尝试,我都无法让它工作。在这种情况下,调用API的正确方法是什么
预期产出:
[
{
kind: 'customsearch#search',
url: {
type: 'application/json',
template: 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json'},
...
},
...
]
由于
singleSearch
async函数返回承诺,因此需要使用then
获取搜索结果,然后将其推送到searches
数组
searches.push(singleSearch(searchQueries[i], engineid, apikey).catch(console.error))
签出Stackoverflow上的答案。问题是您试图在循环中执行异步,但这并没有按照您期望的方式工作。您需要使用
Promise.all
或使用for..of
循环等待。你可以这样做
async function singleSearch(q, cx, auth) {
const res = await customsearch.cse.list({
"cx": cx,
"q": q,
"auth": auth
});
return res.data;
}
function search(words, apikey = process.argv[2], engineid = process.argv[3]) {
const limit = 32; // 32 word limit on google search
const searchQueries = [];
const len = Math.ceil(words.length / limit);
for (var i = 0; i < len; i++) {
searchQueries.push(words.slice(i * limit, (i + 1) * limit).join(" "));
}
return Promise.all(searchQueries.map(query => {
return singleSearch(query, engineid, apikey).catch(console.error);
}));
}
(async () => {
const a = await search("Do I understand it correctly that anotherCall() will be called only when someCall() is completed? What is the most elegant way of calling them in parallel?".split(" "));
console.log(a);
})();
异步函数singleSearch(q、cx、auth){
const res=wait customsearch.cse.list({
“cx”:cx,
“q”:q,
“auth”:auth
});
返回res.data;
}
函数搜索(words,apikey=process.argv[2],engineid=process.argv[3]){
const limit=32;//谷歌搜索的字数限制为32
const searchquerys=[];
const len=Math.ceil(words.length/limit);
对于(变量i=0;i我想询问您是否有兴趣做一些工作,我在网上找到了您的一个回购协议(hookjs),我想知道您是否有兴趣联系,如果有,请回复我在那里打开的问题,谢谢!希望这不会显示为垃圾邮件,我只是不知道如何尝试联系你,因为没有电子邮件,你的网站已关闭。