problem using a Bing Web Search Python client library

Crystal 20 Reputation points
2024-04-24T23:30:08.8166667+00:00

I was trying to use a Bing Web Search Python client library following the instructions on this page https://learn.microsoft.com/en-us/bing/search-apis/bing-web-search/quickstarts/sdk/web-search-client-library-python.

client = WebSearchClient(endpoint="YOUR_ENDPOINT", credentials=CognitiveServicesCredentials(subscription_key))

web_data = client.web.search(query="Yosemite")

Using either "https://api.bing.microsoft.com" or "https://api.bing.microsoft.com/v7.0/search" as endpoint, I got into this error - ErrorResponseException: Operation returned an invalid status code 'Resource Not Found'.

Does anyone have any suggestion to resolve this error? Thanks in advance.

Bing Web Search
Bing Web Search
A Bing service that gives you enhanced search details from billions of web documents.
128 questions
{count} votes

Accepted answer
  1. navba-MSFT 17,280 Reputation points Microsoft Employee
    2024-04-25T03:09:33.4066667+00:00

    @Crystal Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I was able to use the below sample code and that worked fine. Please update the subscription_key and the query in the below code and run the sample:

    import json
    import os
    from pprint import pprint
    import requests
    
    '''
    This sample makes a call to the Bing Web Search API with a query and returns relevant web search.
    '''
    
    # Add your Bing Search V7 subscription key and endpoint to your environment variables.
    subscription_key = "aae3eXXXXXXXXXXXXXXXXXXXffe1c31"
    endpoint = "https://api.bing.microsoft.com" + "/v7.0/search"
    
    # Query term(s) to search for.
    query = "Microsoft Azure Open AI Service"
    
    # Construct a request
    mkt = 'en-US'
    params = { 'q': query, 'mkt': mkt }
    headers = { 'Ocp-Apim-Subscription-Key': subscription_key }
    
    # Call the API
    try:
        response = requests.get(endpoint, headers=headers, params=params)
        response.raise_for_status()
    
        print("\nHeaders:\n")
        print(response.headers)
    
        print("\nJSON Response:\n")
        pprint(response.json())
    except Exception as ex:
        raise ex
    

    Hope this helps. If you have any follow-up questions, please let me know. I would be happy to help.


0 additional answers

Sort by: Most helpful