Bing Image Search API returns empty for non-english queries (Python)

dj1121 26 Reputation points
2022-04-11T23:40:24.6+00:00

I'm trying to use the Bing image search API for a small Python script I'm making. I want to simply search a term in Japanese or Chinese, such as 生姜 and get at least the first image result. My code works fine with English queries and it returns multiple values in the thumbnail_urls list. I have made sure that mkt and set_lang strings are properly assigned. I have also made sure that search_term is not an empty string. I am certain that my subscription key is working, since the code returns results for English. Is there something I am doing wrong when I specify the language such that I get no returned thumbnail urls? Here is the function I am trying to use:

def get_image(search_term, lang):

    if lang == "zh-TW-YunJheNeural":
        mkt = "zh-TW"
        set_lang = "zh-hant"
    else:
        mkt = "ja-JP"
        set_lang = "jp"

    headers = {"Ocp-Apim-Subscription-Key" : IMAGE_KEY}
    params  = {"q": search_term, "license": "public", "imageType": "photo", "mkt": mkt, "setLang": set_lang}
    response = requests.get(SEARCH_URL, headers=headers, params=params)
    search_results = response.json()
    thumbnail_urls = [img["thumbnailUrl"] for img in search_results["value"][:16]]
    print(thumbnail_urls)
Bing Image Search
Bing Image Search
A Bing service that supports searching for images and gets comprehensive results.
45 questions
0 comments No comments
{count} votes

Accepted answer
  1. romungi-MSFT 43,696 Reputation points Microsoft Employee
    2022-04-12T05:37:30.157+00:00

    @dj1121 I think your code to get the results is correct for any market or language is correct. I have tried the same snippet and it does return results for ZH and EN language codes. This is the result for zh-TW

    192069-image.png

    The first image for your search term is the following.

    https://tse2.mm.bing.net/th?id=OIP.DVAVBrd-CTFnlWIbjwGoagHaE3&pid=Api

    I think the issue in your case might be in setting the search term with respect to character encoding that is passed in the request to the API. You could also try the same search using postman to confirm and it should work fine.

    The endpoint I have used in the above case is https://api.bing.microsoft.com/v7.0/images/search

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful