To Get The Official Website URL From A Company Name In Python

INTRODUCTION

This Python code that takes a search term from the user, queries Google Search, and retrieves the first link from the results. It uses the requests library to make the HTTP request and BeautifulSoup to parse the HTML response. After extracting the first search result (since the first website is mostly the official URL) , the program displays the URL and asks if the user wants to open it in a browser. If the user agrees, the webbrowser module is used to open the link.

Working Of Code

Here’s how the code works, step by step:

  1. Input: The user enters a search term.
  2. Request: The script sends the search term to Google.
  3. Parse: It uses BeautifulSoup to find and extract the first link.
  4. Output: The script displays the URL and asks if the user wants to open it.
  5. Action: Based on the user’s input, the browser either opens the link or the program exits.

PROGRAM

Full Program To Get The Official Website URL

from bs4 import BeautifulSoup
import requests
import webbrowser

print("\tEnter Below To Get The Official URL")
name=input("Search Here:")
search=name
url='https://www.google.com/search'

headers = {
    'Accept' : '*/*',
    'Accept-Language': 'en-US,en;q=0.9',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
}
parameters = {'q': search}

content = requests.get(url, headers = headers, params = parameters).text
soup = BeautifulSoup(content, 'html.parser')

search = soup.find(id = 'search')
first_link = search.find('a')

visit=first_link['href']


print("Here Is The Official Website Link:",visit)    
op=input("Enter 'yes' If You Want To Open ("+visit+") Otherwise Enter 'no' To Exit The Program:").lower()

if op=='yes':
    print("Opening....",name)
    webbrowser.open(visit)
else:
    print("Exiting The Program....")

now, lets understand the program in detailed.

Modules Used

from bs4 import BeautifulSoup
import requests
import webbrowser
  • BeautifulSoup: For parsing and navigating HTML content.
  • requests: To send HTTP requests and get the response from Google.
  • webbrowser: To open the search result in the user’s default web browser.

User Input

name = input("Search Here: ")
search = name

The program begins by asking the user to enter a search term using the input() . After the user provides a value, the program immediately stores it in the name variable and also assigns it to search. This value then becomes the key element for performing the Google search query.

Preparing the Request

url = 'https://www.google.com/search'
headers = {
    'Accept': '*/*',
    'Accept-Language': 'en-US,en;q=0.9',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
}
parameters = {'q': search}

Next, the program prepares a Google search request. It sets the base URL to https://www.google.com/search and provides custom headers to imitate a regular browser request. By doing this, the script effectively avoids being blocked by Google’s anti-scraping mechanisms. Additionally, the search term is included in the parameters dictionary, with ‘q’ as the key and the search term as the value.

Sending the Request

content = requests.get(url, headers = headers, params = parameters).text

After setting up the URL and headers, the script proceeds to send a get request to Google using the requests.get() function. The HTML content returned from this request is stored in the content variable. This raw HTML includes the complete Google search results page, providing all the necessary information for further processing.

Parsing the HTML

soup = BeautifulSoup(content, 'html.parser')
search = soup.find(id = 'search')
first_link = search.find('a')

Once the HTML content is available, BeautifulSoup parses it to extract meaningful information. The script specifically looks for an element with the id=’search’, which typically holds the search results. Next, it identifies the first link (the <a> tag) within this element, assuming that this link represents the first search result.

Extracting the URL

visit = first_link['href']

The program then extracts the URL from the href attribute of the first link. It stores this value in the visit variable, which now contains the URL of the first search result. This allows the program to use the extracted link for further actions.

Displaying and Asking User’s Choice

print("Here Is The Official Website Link:", visit)
op = input("Enter 'yes' If You Want To Open (" + visit + ") Otherwise Enter 'no' To Exit The Program: ").lower()

The script prints the extracted URL so the user can see it clearly. Then, it prompts the user to decide whether they want to open the link in a browser. The user responds with either “yes” or “no,” and to ensure consistent comparison, the input is converted to lowercase.

Opening the URL

if op == 'yes':
    print("Opening....", name)
    webbrowser.open(visit)
else:
    print("Exiting The Program....")

If the user enters “yes,” the script opens the extracted link in the browser using webbrowser.open(). Conversely, if the user declines by entering “no,” the program exits and displays a message to indicate that it is closing.

OUTPUT

Opening The URL

Enter Below To Get The Official URL
Search Here:amazon
Here Is The Official Website Link: https://www.amazon.in/
Enter 'yes' If You Want To Open (https://www.amazon.in/) Otherwise Enter 'no' To Exit The Program:yes
Opening.... amazon

Exiting The Program

Enter Below To Get The Official URL
Search Here:amazon
Here Is The Official Website Link: https://www.amazon.in/
Enter 'yes' If You Want To Open (https://www.amazon.in/) Otherwise Enter 'no' To Exit The Program:no
Exiting The Program....

CONCLUSION

In conclusion, this program allows users to conduct a Google search and retrieve the first link from the results. It prompts for a search term, sends a request to Google, and extracts the relevant URL. Users can then choose to open the link in their browser or exit the program.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top