Update scraping_utils.py
Browse files- scraping_utils.py +16 -16
scraping_utils.py
CHANGED
|
@@ -1,27 +1,27 @@
|
|
| 1 |
import requests
|
| 2 |
-
from bs4 import BeautifulSoup
|
| 3 |
|
| 4 |
-
def
|
| 5 |
"""
|
| 6 |
-
Perform a
|
| 7 |
-
Each result includes the title, link, and description.
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
|
|
|
|
| 15 |
if response.status_code != 200:
|
| 16 |
raise Exception(f"Failed to fetch search results: {response.status_code}")
|
| 17 |
|
| 18 |
-
|
| 19 |
results = []
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
return results
|
|
|
|
| 1 |
import requests
|
|
|
|
| 2 |
|
| 3 |
+
def search_bing(query):
|
| 4 |
"""
|
| 5 |
+
Perform a search using Bing Search API and return a list of results.
|
| 6 |
+
Each result includes the title, link, and snippet (description).
|
| 7 |
"""
|
| 8 |
+
# Replace 'YOUR_BING_API_KEY' with your Bing Search API key
|
| 9 |
+
api_key = "YOUR_BING_API_KEY"
|
| 10 |
+
endpoint = "https://api.bing.microsoft.com/v7.0/search"
|
| 11 |
+
headers = {"Ocp-Apim-Subscription-Key": api_key}
|
| 12 |
+
params = {"q": query, "count": 5, "textFormat": "Raw"}
|
| 13 |
|
| 14 |
+
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
| 15 |
if response.status_code != 200:
|
| 16 |
raise Exception(f"Failed to fetch search results: {response.status_code}")
|
| 17 |
|
| 18 |
+
data = response.json()
|
| 19 |
results = []
|
| 20 |
+
for result in data.get("webPages", {}).get("value", []):
|
| 21 |
+
results.append({
|
| 22 |
+
"title": result["name"],
|
| 23 |
+
"link": result["url"],
|
| 24 |
+
"snippet": result["snippet"]
|
| 25 |
+
})
|
| 26 |
|
| 27 |
return results
|