javascript - 使用 Bing Image API Javascript 搜索图像
问题描述
我正在尝试使用 Bing 搜索图像 API 来搜索我调用的一系列项目names
。我已使用https://docs.microsoft.com/en-us/azure/cognitive-services/bing-image-search/quickstarts/nodejs上的文档开始发送 JSON 解析请求。单独调用名称时,我能够接收回图像 URL,但是,当我bing_image_search
通过for
循环运行该函数时,控制台会打印出它找不到图像结果,如果确实找到其中一些,则不会返回它们为了使bing_image_search
函数调用每个名称。下面是显示我现在要做的代码。任何帮助表示赞赏。
var imagesArr = [];
let num_results = 1;
var names = []; // assume already filled with names
let response_handler = function (response) {
let body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
let imageResults = JSON.parse(body);
if (imageResults.value) {
let imageLink = imageResults.value[0].thumbnailUrl;
//console.log(`Image result count: ${imageResults.value.length}`);
//imagesArr.push(imageLink);
//console.log(imageLink);
return imageLink;
}
else {
console.log("Couldn't find image results!");
}
});
response.on('error', function (e) {
console.log('Error: ' + e.message);
});
};
let bing_image_search = function (search) {
console.log('Searching images for: ' + search);
let request_params = {
method : 'GET',
hostname : host,
path : path + '?q=' + encodeURIComponent(search) + '&count=' + num_results,
headers : {
'Ocp-Apim-Subscription-Key' : subscriptionKey,
}
};
let req = https.request(request_params, response_handler);
req.end();
}
for(index in names) {
bing_image_search(names[index]);
}
解决方案
您可能需要考虑将 bing_image_search 逻辑包装在一个 Promise 中,以便更轻松地控制流程......看看下面的内容是否对您有帮助:
const { RateLimiter } = require("limiter");
const limiter = new RateLimiter({ tokensPerInterval: 3, interval: "second" });
var imagesArr = [];
let num_results = 1;
var names = []; // assume already filled with names
function bing_image_search(search) {
return new Promise(async (resolve,reject)=>{
const remainingMessages = await limiter.removeTokens(1);
console.log('Searching images for: ' + search);
let request_params = {
method : 'GET',
hostname : host,
path : path + '?q=' + encodeURIComponent(search) + '&count=' + num_results,
headers : {
'Ocp-Apim-Subscription-Key' : subscriptionKey,
}
};
let req = https.request(request_params, (response)=>{
let body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
let imageResults = JSON.parse(body);
if (imageResults.value) {
let imageLink = imageResults.value[0].thumbnailUrl;
//console.log(`Image result count: ${imageResults.value.length}`);
//imagesArr.push(imageLink);
//console.log(imageLink);
resolve(imageLink);
}
else {
resolve("Couldn't find image results!");
}
});
response.on('error', function (e) {
reject(e);
});
});
req.on('error', (e) => {
reject(e);
});
req.end();
})
}
(async()=>{
let imgUrls = []
for (const name of names){
let imgurl = await bing_image_search(name)
imgUrls.push(imgurl)
}
})()
推荐阅读
- linux - 我可以在没有 /dev/tty* 的情况下使用 am335x 用户 uart 吗?
- javascript - Elasticsearch 过滤动态聚合字段
- python - python math.log(x,base) 结果的精度错误
- c++ - c++:警告比较迭代器
- python - 在“if”条件失败或继续的情况下重新运行函数
- .htaccess - 我有一个 wordpress 多站点,我想从不同域的一个站点加载内容
- apache-spark - 我可以从 Solr 读取 Spark 流吗?
- r - c_across 使用 dplyr 每行的多个函数
- ajax - Axios 和 Wufoo:从 React 发布时“不允许请求标头字段授权”
- xml - XSLT Serach 在匹配属性的元素列表中