-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
90 lines (76 loc) · 2.71 KB
/
file.py
File metadata and controls
90 lines (76 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
import requests
import json
def print_banner():
banner = """
============================================================
Google Search Query Automator
Powered by Google Custom Search API
============================================================
"""
print(banner)
def google_search(api_key, cse_id, query, num_results=10):
url = "https://www.googleapis.com/customsearch/v1"
params = {
'key': api_key,
'cx': cse_id,
'q': query,
'num': min(num_results, 10)
}
print("[INFO] Searching for: {}".format(query))
try:
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
results = []
for item in data.get("items", []):
result = {
'title': item.get("title"),
'link': item.get("link"),
'snippet': item.get("snippet")
}
results.append(result)
return results
else:
print("[ERROR] HTTP Error: {}".format(response.status_code))
return []
except Exception as e:
print("[ERROR] Exception during search: {}".format(e))
return []
def display_results(results):
if not results:
print("[INFO] No results found!")
return
print("\nSearch Results:")
print("=" * 50)
for idx, result in enumerate(results, start=1):
title = result['title'].encode('utf-8', 'ignore')
link = result['link']
snippet = result['snippet'].encode('utf-8', 'ignore')
print("[{}] {}".format(idx, title))
print(" URL: {}".format(link))
print(" Snippet: {}\n".format(snippet))
print("=" * 50)
def main():
print_banner()
# Enter API Key and CSE ID
api_key = "apikey_" # Replace with your actual API Key
cse_id = "cse_id" # Replace with your actual CSE ID
# Input search keywords and number of results
query = raw_input("Enter your search query: ").strip()
while True:
try:
num_results = int(raw_input("Enter the number of results you want (1-10): "))
if 1 <= num_results <= 10:
break
else:
print("Please enter a number between 1 and 10.")
except ValueError:
print("Invalid input. Please enter a number.")
results = google_search(api_key, cse_id, query, num_results)
display_results(results)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n[INFO] Exiting...")