|
| 1 | +from googlesearch import search |
| 2 | +import sys |
| 3 | + |
| 4 | +class googlesearch: |
| 5 | + |
| 6 | + def __init__(self): |
| 7 | + self.search_count = None |
| 8 | + self.keyword_to_search = None |
| 9 | + self.search_results = None |
| 10 | + |
| 11 | + def set_search_count(self): |
| 12 | + # Set the number of search results to display |
| 13 | + print("Enter the count of search results you want to display:") |
| 14 | + count = int(input()) |
| 15 | + self.search_count = count |
| 16 | + |
| 17 | + def set_search_text(self): |
| 18 | + # Set the keyword to search on Google |
| 19 | + print("Enter the text you want to search on Google:") |
| 20 | + text = input() |
| 21 | + self.keyword_to_search = text |
| 22 | + |
| 23 | + def perform_search(self): |
| 24 | + try: |
| 25 | + # Perform the Google search |
| 26 | + results = search(self.keyword_to_search, num_results=self.search_count) |
| 27 | + self.search_results = results |
| 28 | + except Exception as e: |
| 29 | + print(e) |
| 30 | + print("Google search faced an exception") |
| 31 | + |
| 32 | + print("Google search performed successfully") |
| 33 | + |
| 34 | + def print_search_res(self): |
| 35 | + print("The search results for {} keyword are:".format(self.keyword_to_search)) |
| 36 | + |
| 37 | + count = 1 |
| 38 | + for search_res in self.search_results: |
| 39 | + # Print the search results |
| 40 | + print("Search No {}: {}".format(count, search_res)) |
| 41 | + count += 1 |
| 42 | + |
| 43 | + |
| 44 | +def main(): |
| 45 | + google_search_bot = googlesearch() |
| 46 | + |
| 47 | + while True: |
| 48 | + print("Select any one of the valid operations which are listed below:") |
| 49 | + print("1. To set the number of searches we need to display for each Google Search.") |
| 50 | + print("2. To enter the keyword for the Google Search.") |
| 51 | + print("3. To perform Google Search for the keyword entered by the user.") |
| 52 | + print("4. To print the Google search results obtained after searching.") |
| 53 | + print("5. To exit from the code execution.") |
| 54 | + |
| 55 | + choice = int(input()) |
| 56 | + |
| 57 | + if choice == 1: |
| 58 | + google_search_bot.set_search_count() |
| 59 | + elif choice == 2: |
| 60 | + google_search_bot.set_search_text() |
| 61 | + elif choice == 3: |
| 62 | + google_search_bot.perform_search() |
| 63 | + elif choice == 4: |
| 64 | + google_search_bot.print_search_res() |
| 65 | + elif choice == 5: |
| 66 | + sys.exit() |
| 67 | + |
| 68 | + print("To continue with the code execution, enter 'y' or 'n':") |
| 69 | + continue_or_exit = input() |
| 70 | + |
| 71 | + if continue_or_exit == 'n' or continue_or_exit == 'N': |
| 72 | + sys.exit() |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + main() |
0 commit comments