Untitled

 avatar
unknown
plain_text
a year ago
947 B
4
Indexable
from urllib.parse import urlencode
from urllib.request import urlopen
import json

def google_search(search_terms, api_key, cse_id, **kwargs):
    # create the google search url
    base_url = "https://www.googleapis.com/customsearch/v1"
    params = {
        "q": search_terms,
        "key": api_key,
        "cx": cse_id,
    }
    params.update(kwargs)
    url = base_url + "?" + urlencode(params)

    # get the search results
    response = urlopen(url)
    search_results = json.loads(response.read().decode())

    return search_results

# set your API key and custom search engine ID
api_key = "YOUR_API_KEY"
cse_id = "YOUR_CSE_ID"

# use the function to search google
search_terms = "Python programming"
search_results = google_search(search_terms, api_key, cse_id)

# print the results
for i, result in enumerate(search_results["items"], start=1):
    print(f"{i}. {result['title']}\n{result['link']}\n")
Editor is loading...