from googlesearch import search def get_website(company_name): combine = f"{company_name} official website" for i in search(combine,num_results=1): print(i) while True: print("MENU") print("1. Get The Official Website URL") print("2. Exit The Program") options=int(input("Enter The Option[1/2]:")) if options==1: company_name=input("Enter The Name Of The Company:") get_website(company_name) elif options==2: print("Exiting The Program") break else: print("Invalid Option!!! Please Choose 1 or 2.")
Now, let’s understand this program step by step.
Import the search function:
from googlesearch import search
This imports the search function from the googlesearch-python library. The search function is used to perform a Google search directly from Python.
Defining the Function:
def get_website(company_name): combine = f"{company_name} official website" for i in search(combine,num_results=1): print(i)
After importing the function defining a get_website(company_name) function, this function takes company_name as an input. combine=f”{company_name} official website”, the combine variable combining the company name with the word “official website”.With the help of this Google will return the official website as the top result. search loop: for i in search(combine, num_results=1): performs the Google search using the search function, (num_results=1) returning only one result.
Enter the main loop:
while True: print("MENU") print("1. Get The Official Website URL") print("2. Exit The Program") options=int(input("Enter The Option[1/2]:")) if options==1: company_name=input("Enter The Name Of The Company:") get_website(company_name) elif options==2: print("Exiting The Program") break else: print("Invalid Option!!! Please Choose 1 or 2.")
Next, the program enters a while True loop, which allows continuous operation until the user chooses to exit. Inside this loop, it first displays the title of the MENU along with two options:
- Option 1: Get The Official Website URL.
- Option 2: Exit the program.
After this, the program asks the user to enter your option(1 or 2). If the user selects option 1, the program asks the user to enter the company name. After the user provides the company name, the program calls the get_website(company_name) function to get the official website of the company. If the user selects option 2, the program prints “Exiting The Program” and breaks out of the loop, effectively terminating the program. If the user enters anything other than 1 or 2, the program displays an Invalid Option!!! please choose 1 or 2.
Output:
MENU
1. Get The Official Website URL
2. Exit The Program
Enter The Option[1/2]:1
Enter The Name Of The Company: Codespeedy
https://www.codespeedy.com/
MENU
1. Get The Official Website URL
2. Exit The Program
Enter The Option[1/2]:5
Invalid Option!!! Please Choose 1 or 2.
MENU
1. Get The Official Website URL
2. Exit The Program
Enter The Option[1/2]:2
Exiting The Program