Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Created October 25, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuriyanto48/02837b2485ea088f6faa85212a845ec4 to your computer and use it in GitHub Desktop.
Save wuriyanto48/02837b2485ea088f6faa85212a845ec4 to your computer and use it in GitHub Desktop.
Google Search Python
import requests
import json
api_key = ""
cx = ""
query = "pertamina berita"
url = f"https://www.googleapis.com/customsearch/v1?key={api_key}&cx={cx}&q={query}"
response = requests.get(url)
data = json.loads(response.text)
# Check for errors or empty search results
if 'error' in data:
print("Error:", data['error']['message'])
elif 'items' not in data:
print("No search results found.")
else:
# Extract search results
search_results = data['items']
print(search_results)
print(len(search_results))
for s in search_results:
print('-----')
print(s['pagemap'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment