0

我目前正在做一个使用 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

谢谢您的帮助 :)

4

1 回答 1

0

该问题与 Bing Image 搜索没有直接关系,不幸的是,您已经在 node.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

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

Dorm%C3%A9是 Dormé 的 URL 编码版本,当您在 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现在返回以下内容:

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

于 2019-06-11T15:38:21.550 回答