Here we are going to write a code in python. we can get one company official URL . we can easily enter in to the company by clicking the URL This code very easy to understand and very simplistic.
we easily get the logic that how to get a particular company URL
ALGORITHM
- Get company name from user input
- Convert input to lowercase for case-insensitive search unavailable))
- Append company name to search URL as query parameter (q={company_name})
- Use requests.get() to send HTTP GET request to search URL
- Handle potential exceptions using try-except block
- Check HTTP response status code using response.raise_for_status()
- HTML content using BeautifulSoup library
- Find all anchor tags (<a>) in parsed HTML
- Iterate through links and check if company name is present in href attribute
- Extract matching link’s href attribute value
- Construct official website URL by concatenating https://www.{company_name}.com with extracted href value
- Print official website URL
- Catch requests.exceptions.RequestException and print error message
- Print message indicating inability to find official website
Source code
-
import requests from bs4 import BeautifulSoup company_name=input("enter the name to search : ").lower() search_url=f"https://www.apple.com/search?q={company_name}" try: response=requests.get(search_url) response.raise_for_status() soup=BeautifulSoup(response.content,"html.parser") for link in soup.find_all("a"): if company_name.lower() in link.get("href",'').lower(): website_url=link.get("href") print(f"the official website for {company_name} is https://www.{company_name}.com{website_url}") break except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") print(f"could not find the the official website for {company_name}")
output
-
https://www.apple.com/search?q=apple&sca_esv=89b9e73d172150e9&sca_upv=1&ie=UTF-8&gbv=1&sei=uxjwZq6yOqHn2roP2JHpmAo