Created
October 25, 2023 13:14
-
-
Save wuriyanto48/02837b2485ea088f6faa85212a845ec4 to your computer and use it in GitHub Desktop.
Google Search Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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