如何设置 bing 图片搜索选项 api

How to set options for the bing image search api

我目前正在做一个使用 bing 图片搜索 api 的学校项目。我的目标是为每个星球大战角色获取个人资料图片(尽可能准确)。我不明白为什么,但是 api 似乎找不到带有重音字符的结果。

例如,在下面的代码中,当 searchTerm = "Han Solo" 时一切正常,但是当 searchTerm = "Dormé" 时 API 没有 return 任何图片。

奇怪的是,如果我直接在 bing 中进行相同的搜索,它会找到很多图片,但我从 API.

中得到 none
//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

感谢您的帮助:)

该问题与 Bing 直接图像搜索无关,不幸的是,您在 node.js(以及一般网络)[=17] 中打开了一个围绕字符编码的可爱蠕虫罐头=]

为了演示问题,首先编写一个更简单的方法:

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

    // will return Dorm%C3%A9
    res.send(searchTerm);
});

Dorm%C3%A9 是 URL-encoded 版本的 Dormé,当您在 url 栏中输入它时,您的浏览器会自动生成它。您可以使用在线 url-decode 网站验证这一点,例如:https://urldecode.org/?text=Dorm%25C3%25A9&mode=decode

因此,您需要使用 decodeURI 函数在您的代码中解码此值。

const sendQuery = async () => {
    return await imageSearchClient.imagesOperations.search(decodeURI(searchTerm));
};

现在请求 http://localhost:3000/getPortrait/Dorm%C3%A9 现在 returns 以下内容:

http://vignette2.wikia.nocookie.net/starwars/images/1/18/Dormesenate.jpg/revision/latest?cb=20070506233854