File size: 946 Bytes
0badc43
 
1fb3118
daed239
1fb3118
 
daed239
1fb3118
 
 
 
 
0badc43
1fb3118
596a243
daed239
596a243
1fb3118
daed239
1fb3118
 
 
 
 
 
596a243
daed239
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests

def search_bing(query):
    """
    Perform a search using Bing Search API and return a list of results.
    Each result includes the title, link, and snippet (description).
    """
    # Replace 'YOUR_BING_API_KEY' with your Bing Search API key
    api_key = "YOUR_BING_API_KEY"
    endpoint = "https://api.bing.microsoft.com/v7.0/search"
    headers = {"Ocp-Apim-Subscription-Key": api_key}
    params = {"q": query, "count": 5, "textFormat": "Raw"}

    response = requests.get(endpoint, headers=headers, params=params, timeout=10)
    if response.status_code != 200:
        raise Exception(f"Failed to fetch search results: {response.status_code}")

    data = response.json()
    results = []
    for result in data.get("webPages", {}).get("value", []):
        results.append({
            "title": result["name"],
            "link": result["url"],
            "snippet": result["snippet"]
        })

    return results