Google search API
· API ·Custom Search JSON API · Programmable search engine
Getting Started With Google Search API For Beginners In Python | Full Tutorial
import cachetools.func, requests, json, re, logging
@cachetools.func.ttl_cache(maxsize=50, ttl=3600)
def google_api_search(search_query: str):
g_api_key = 'api key from https://developers.google.com/'
g_search_engine = 'serch engine id from https://programmablesearchengine.google.com/'
num = 5
payload = {
'key': g_api_key,
'q': search_query,
'cx': g_search_engine,
'start': 1,
'num': num,
'dateRestrict': 'm1'
}
response = requests.get('https://www.googleapis.com/customsearch/v1', params = payload)
if response.status_code != 200:
logging.error(f'Google api search failed with code {response.status_code}')
return None
else:
jr = response.json()
if jr.get('items'):
return jr['items']
else:
logging.error(f'Google api search returned no items')
return None