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

dj1121 26 Reputation points

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"
        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]]
Bing Image Search
Bing Image Search
A Bing service that supports searching for images and gets comprehensive results.
44 questions
0 comments No comments
{count} votes

Accepted answer
  1. romungi-MSFT 43,451 Reputation points Microsoft Employee

    @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


    The first image for your search term is the following.

    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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful