forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
79 lines (61 loc) · 2.42 KB
/
code.py
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
from googlesearch import search
import sys
class googlesearch:
def __init__(self):
self.search_count = None
self.keyword_to_search = None
self.search_results = None
def set_search_count(self):
# Set the number of search results to display
print("Enter the count of search results you want to display:")
count = int(input())
self.search_count = count
def set_search_text(self):
# Set the keyword to search on Google
print("Enter the text you want to search on Google:")
text = input()
self.keyword_to_search = text
def perform_search(self):
try:
# Perform the Google search
results = search(self.keyword_to_search,
num_results=self.search_count)
self.search_results = results
except Exception as e:
print(e)
print("Google search faced an exception")
print("Google search performed successfully")
def print_search_res(self):
print("The search results for {} keyword are:".format(
self.keyword_to_search))
count = 1
for search_res in self.search_results:
# Print the search results
print("Search No {}: {}".format(count, search_res))
count += 1
def main():
google_search_bot = googlesearch()
while True:
print("Select any one of the valid operations which are listed below:")
print("1. To set the number of searches we need to display for each Google Search.")
print("2. To enter the keyword for the Google Search.")
print("3. To perform Google Search for the keyword entered by the user.")
print("4. To print the Google search results obtained after searching.")
print("5. To exit from the code execution.")
choice = int(input(''))
if choice == 1:
google_search_bot.set_search_count()
elif choice == 2:
google_search_bot.set_search_text()
elif choice == 3:
google_search_bot.perform_search()
elif choice == 4:
google_search_bot.print_search_res()
else choice == 5:
sys.exit()
print("To continue with the code execution, enter 'y' or 'n':")
continue_or_exit = input()
if continue_or_exit == 'n' or continue_or_exit == 'N':
sys.exit()
if __name__ == '__main__':
main()