|
|
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). |
|
|
""" |
|
|
|
|
|
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 |
|
|
|