Get official URL of any company using Python

In this tutorial, I will show you how to fetch or get the URL of official website of any company using Python. We will use beautifulsoup and requests module to do this.

Logic behind this:

  • We will first make a Google search for a specific keyword ( here its the company name ) through Beautifulsoup library in Python.
  • Then we will pass the keyword to Google search parameter. (Which looks like this https://www.google.com/search?q=keyword)
  • Now, we will fetch the URL of the first search result. (As in 99.99% of cases, the first URL is the official website)

Python code to fetch URL of any company’s official website

from bs4 import BeautifulSoup
import requests

search = "codespeedy"
url = 'https://www.google.com/search'

headers = {
    'Accept' : '*/*',
    'Accept-Language': 'en-US,en;q=0.5',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82',
}
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')

print(first_link['href'])

Output:

https://www.codespeedy.com/

You can see I have stored “codespeedy” string in the search variable, line number 4.

To fetch the first link, I have used: first_link = search.find('a'). Here ‘a’ is for finding <a href tag which is a link.

I can also fetch URL of a lot of websites from a CSV file or Excel file dataset too.

By modifying this code we can get the URL of thousands of company’s in a single program.

If you need any custom code for related to this feel free to contact me. I will do that for you. You can just click on the contact button of this website or send an email to us.

Leave a Reply

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