BrandSight / src /google_search.py
akashjayampu's picture
Create google_search.py
b4feefb verified
raw
history blame
442 Bytes
import os
import requests
def google_search(query, num=5):
api_key = os.getenv("GOOGLE_API_KEY")
cse_id = os.getenv("GOOGLE_CSE_ID")
url = "https://www.googleapis.com/customsearch/v1"
params = {
"key": api_key,
"cx": cse_id,
"q": query,
"num": num
}
res = requests.get(url, params=params).json()
return [item["snippet"] + "\nURL: " + item["link"] for item in res.get("items", [])]