Spaces:
Sleeping
Sleeping
Create search_utils.py
Browse files- search_utils.py +23 -0
search_utils.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
|
4 |
+
SERPAPI_KEY = "e41a265c89513f03e569eda056f6f50374332cd6c76feb1662baf401c7adb564"
|
5 |
+
|
6 |
+
def web_search(query, num_results=3):
|
7 |
+
url = "https://serpapi.com/search"
|
8 |
+
params = {
|
9 |
+
"engine": "google",
|
10 |
+
"q": query,
|
11 |
+
"api_key": SERPAPI_KEY,
|
12 |
+
"num": num_results
|
13 |
+
}
|
14 |
+
|
15 |
+
response = requests.get(url, params=params)
|
16 |
+
if response.status_code != 200:
|
17 |
+
return []
|
18 |
+
|
19 |
+
results = response.json().get("organic_results", [])
|
20 |
+
return [
|
21 |
+
f"{res.get('title')}: {res.get('snippet')}"
|
22 |
+
for res in results if res.get("title") and res.get("snippet")
|
23 |
+
]
|