我目前正在做一个使用 bing 图像搜索 api 的学校项目。我的目标是获取每个星球大战角色的个人资料图片(尽可能准确)。我不明白为什么,但是 api 似乎没有找到带有重音字符的结果。
例如,在下面的代码中,当searchTerm = "Han Solo"
一切正常但searchTerm = "Dormé"
API 没有返回任何图片时。
奇怪的是,如果我直接在 bing 中进行相同的搜索,它会找到很多图片,但我从 API 中没有得到任何图片。
//server.js
app.get('/getPortrait/*', (req, res) => {
const serviceKey = "MY_KEY";
let searchTerm = req.url.split('/').pop();
// when searchTerme= "han solo", it works fine
// when searchTerme= "Dormé", it works but returns no image in the response
let credentials = new CognitiveServicesCredentials(serviceKey);
let imageSearchClient = new Search.ImageSearchClient(credentials);
let resultURL;
const sendQuery = async () => {
return await imageSearchClient.imagesOperations.search(searchTerm);
};
sendQuery().then(imageResults =>{
console.debug(imageResults)
if (imageResults == null || imageResults.value.length == 0) {
console.error("No image results were found.");
res.send(defaultPic);
}
else {
resultURL = imageResults.value[0].contentUrl;
console.log(resultURL);
res.send(resultURL);
}
}).catch(err => {
console.error(err)
res.send(defaultPic);
});
});
有没有办法将搜索配置为接受所有类型的字符?
以下是我从这些查询中得到的结果:
\\searchTerm = "Dormé"
Object {_type: "Images", value: Array(0)}
_type:"Images"
value:Array(0) []
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
\\searchTerm = "han solo"
Object {_type: "Images", readLink: "https://api.cognitive.microsoft.com/api/v7/images/…", webSearchUrl: "https://www.bing.com/images/search?q=han%20solo&FO…", totalEstimatedMatches: 868, nextOffset: 43, …}
nextOffset:43
readLink:"https://api.cognitive.microsoft.com/api/v7/images/search?q=han%20solo"
totalEstimatedMatches:868
value:Array(35) [Object, Object, Object, …]
webSearchUrl:"https://www.bing.com/images/search?q=han%20solo&FORM=OIIARP"
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
https://www.starwarsnewsnet.com/wp-content/uploads/2017/01/Alden-Ehrenreich-as-Han-Solo-4.jpg
谢谢您的帮助 :)