question

dj1121 avatar image
0 Votes"
dj1121 asked dj1121 commented

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

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)
azure-bing-image
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

romungi-MSFT avatar image
1 Vote"
romungi-MSFT answered dj1121 commented

@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



image.png (51.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the response. The error is indeed one about encoding, however, I'm not sure how to fix it. If I use a literal string in the q field of the params (like "生姜"), then it indeed works. So, how do I make my variable search_term work in the same manner? I am reading the file lines using utf-8 encoding and then passing the lines to this function. I know the problem is somewhere related to this, since I can do search_term = "生姜“ (i.e. assigning a literal) and it will work.

0 Votes 0 ·

Actually, it seems there was no encoding issue at all. Instead, my search term was simply too long. Now I just work my way from the end of the string backwards until a result is found. Thanks!

1 Vote 1 ·