"Operation returned an invalid status code 'Resource Not Found'"

Sai Kishore 0 Reputation points
2024-06-03T06:08:53.4066667+00:00

i followed the documentation to create the bing web search but i got error as resource not found error i can't find any solution for that error.
reference:

def get_profiles(self):
            try:
                client = WebSearchClient(endpoint="https://api.bing.microsoft.com/", credentials=CognitiveServicesCredentials("api_key"))
                web_data = client.web.search(query="python")
                if web_data.web_pages.value:
                    print("Webpage Results#{}".format(len(web_data.web_pages.value)))
                    first_web_page = web_data.web_pages.value[0]
                    print("First web page name: {} ".format(first_web_page.name))
                    print("First web page URL: {} ".format(first_web_page.url))
  
            except Exception as e:
   				print(e)

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

1 answer

Sort by: Most helpful
  1. navba-MSFT 19,655 Reputation points Microsoft Employee
    2024-06-03T07:29:31.2066667+00:00

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

    .

    I was able to use the below python sample code and call the Bing Search endpoint. Could you please test the same and check if that helps ?

    .
    Please note, update the subscription key (api-key) and the query text accordingly.

    .

    import requests
    
    subscription_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
    search_url = "https://api.bing.microsoft.com/v7.0/search"
    headers = {"Ocp-Apim-Subscription-Key": subscription_key}
    params = {"q": "
    
    response = requests.get(search_url, headers=headers, params=params)
    response.raise_for_status()
    search_results = response.json()
    
    webpages = search_results.get('webPages', {}).get('value', [])
    
    for i, webpage in enumerate(webpages):
        name = webpage.get('name')
        url = webpage.get('url')
        snippet = webpage.get('snippet')
        print(f"Result {i+1}:\nTitle: {name}\nURL: {url}\nSnippet: {snippet}\n{'-'*50}")
    
    # If there are no results
    if not webpages:
        print("No results found.")
    

    .

    Hope this helps. If you have any follow-up questions, please let me know. I would be happy to help. . ** Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments