Home » Tutorials » How to Build a Username Search Tool in Python

How to Build a Username Search Tool in Python

Ever wondered if your favorite username is taken on social media?

Today, we’re diving into a fun and practical project: building a Username Search Tool using Python. With this tool, you’ll be able to quickly find out if a username is available across different social media sites, and even grab the profile URL if it exists. Get ready to mix a bit of coding with a dash of creativity, as we make your online identity search easier than ever!

Let’s get started!

Table of Contents

Necessary Libraries

First, make sure to install these libraries via the terminal or your command prompt by running these commands for the code to function properly:

pip install tk 
pip install requests 
pip install beautifulsoup4 
pip install pyperclip

Imports

We start by importing the tkinter library, which enables us to create a graphical user interface. From tkinter, we also import ttk for themed widgets and messagebox to display message boxes. Next, we import the requests library to make HTTP requests to web pages.

Following that, we import BeautifulSoup from beautifulsoup4 to parse HTML content. Lastly, we import pyperclip to enable copying the resulting link to the clipboard.

import tkinter as tk
from tkinter import ttk, messagebox
import requests
from bs4 import BeautifulSoup
import pyperclip

Platform Dictionary

Next, we create a dictionary named platforms that contains key-value pairs: the keys represent the names of the platforms where the user wants to search for a username, and the values are the URLs of these platforms.

# Define the platforms dictionary
platforms = {
   "Instagram": "https://www.instagram.com/",
   "Facebook": "https://www.facebook.com/",
   "Reddit": "https://www.reddit.com/user/",
   "Twitter": "https://twitter.com/",
   "TikTok": "https://www.tiktok.com/@",
   "Pinterest": "https://www.pinterest.com/",
   "GitHub": "https://github.com/",
   "NewPlatform": "https://www.newplatform.com/"  # Add the new platform here replace this line with the new platform
   # Add more platforms as needed if you add more than one platform don't forget to add "," at the end of the line and only leave the last platform without one
}

Username Seach Tool Functions

Now, let’s define our functions:

check_availability Function

This function begins by retrieving the inputs, which are the username and platform. It then constructs a specific URL by combining the platform’s URL with the username. Next, it uses the requests library to make an HTTP GET request to this URL. If successful (status code of 200), it retrieves the HTML content.

The function then parses this HTML content using BeautifulSoup to determine whether the username exists. Once this determination is made, it updates a text widget in the GUI to display the result. If the function fails to check the username’s availability, an error message is displayed instead.

def check_availability():
   username = username_entry.get()
   selected_platform = platform_combobox.get()


   if selected_platform:
       if selected_platform in platforms:
           url = platforms[selected_platform] + username
           response = requests.get(url)
           if response.status_code == 200:
               soup = BeautifulSoup(response.text, "html.parser")
               exists = True


               # Dynamically handle availability based on platform
               if selected_platform in ["Instagram", "TikTok"]:
                   exists = not soup.find("h2", string="Sorry, this page isn't available.")
               elif selected_platform in ["Facebook", "GitHub"]:
                   exists = not soup.find("title").text.startswith("Page Not Found")
               elif selected_platform == "Reddit":
                   exists = not soup.find("span", class_="error")
               elif selected_platform == "Twitter":
                   exists = "Sorry, that page doesn’t exist!" not in soup.text
               elif selected_platform == "Pinterest":
                   exists = "Sorry, we couldn't find that page" not in soup.text
               elif selected_platform == "NewPlatform":  # Condition for the new platform
                   # Add availability check logic specific to the new platform here
                   pass  # Placeholder, replace this with actual logic and be sure to remove pass


               availability_text = f"{username} is {'available' if exists else 'taken'} on {selected_platform}.\n"
               availability_text += f"Profile link: {url}"
               availability_text_widget.delete(1.0, tk.END)
               availability_text_widget.insert(tk.END, availability_text)
           else:
               availability_text_widget.delete(1.0, tk.END)
               availability_text_widget.insert(tk.END, f"Failed to check {selected_platform} availability.")
       else:
           availability_text_widget.delete(1.0, tk.END)
           availability_text_widget.insert(tk.END, "Please select a valid platform.")
   else:
       availability_text_widget.delete(1.0, tk.END)
       availability_text_widget.insert(tk.END, "Please select a platform.")

copy_url Function

The first action of this function is to retrieve the URL displayed in the availability text widget by the previous function and then copy it to the clipboard using pyperclip.

def copy_url():
   url = availability_text_widget.get("2.0", "end-1c")  # Adjusted indexing
   if url:
       pyperclip.copy(url)
       messagebox.showinfo("Copied", "URL copied to clipboard.")

Creating the Main Window

After that, we create the main window, where we will place our widgets. We will also set its title and define its geometry.

# Tkinter GUI setup
root = tk.Tk()
root.title("Username Availability Checker - The Pycodes")
root.geometry("450x300")

Setting Up the GUI Elements

For this step, we start by creating a label that prompts the user to enter a username. Next to this label, we place an entry box for the user to do so. Below this, another label asks the user to select a platform, accompanied by a combobox filled with a list of platforms to choose from.

After that, we create two buttons: the “Check Availability” button, which triggers the check_availability() function, and the “Copy URL” button, which activates the copy_url() function.

Finally, we introduce the availability_text_widget to display the URL.

username_label = tk.Label(root, text="Enter username:")
username_label.grid(row=0, column=0)


username_entry = tk.Entry(root, width=23)  # Adjusted width
username_entry.grid(row=0, column=1)


platform_label = tk.Label(root, text="Select platform:")
platform_label.grid(row=1, column=0)


platform_combobox = ttk.Combobox(root, values=list(platforms.keys()))  # Dynamic platform options
platform_combobox.grid(row=1, column=1)


check_button = tk.Button(root, text="Check Availability", command=check_availability)
check_button.grid(row=2, column=0)


copy_button = tk.Button(root, text="Copy URL", command=copy_url)
copy_button.grid(row=2, column=1)


availability_text_widget = tk.Text(root, height=10, width=50)
availability_text_widget.grid(row=3, columnspan=2,padx=20)

Main Loop

Lastly, this part ensures that the main window remains operational and responsive to user interactions until they decide to exit.

root.mainloop()

Example

Full Code

import tkinter as tk
from tkinter import ttk, messagebox
import requests
from bs4 import BeautifulSoup
import pyperclip


# Define the platforms dictionary
platforms = {
   "Instagram": "https://www.instagram.com/",
   "Facebook": "https://www.facebook.com/",
   "Reddit": "https://www.reddit.com/user/",
   "Twitter": "https://twitter.com/",
   "TikTok": "https://www.tiktok.com/@",
   "Pinterest": "https://www.pinterest.com/",
   "GitHub": "https://github.com/",
   "NewPlatform": "https://www.newplatform.com/"  # Add the new platform here replace this line with the new platform
   # Add more platforms as needed if you add more than one platform don't forget to add "," at the end of the line and only leave the last platform without one
}


def check_availability():
   username = username_entry.get()
   selected_platform = platform_combobox.get()


   if selected_platform:
       if selected_platform in platforms:
           url = platforms[selected_platform] + username
           response = requests.get(url)
           if response.status_code == 200:
               soup = BeautifulSoup(response.text, "html.parser")
               exists = True


               # Dynamically handle availability based on platform
               if selected_platform in ["Instagram", "TikTok"]:
                   exists = not soup.find("h2", string="Sorry, this page isn't available.")
               elif selected_platform in ["Facebook", "GitHub"]:
                   exists = not soup.find("title").text.startswith("Page Not Found")
               elif selected_platform == "Reddit":
                   exists = not soup.find("span", class_="error")
               elif selected_platform == "Twitter":
                   exists = "Sorry, that page doesn’t exist!" not in soup.text
               elif selected_platform == "Pinterest":
                   exists = "Sorry, we couldn't find that page" not in soup.text
               elif selected_platform == "NewPlatform":  # Condition for the new platform
                   # Add availability check logic specific to the new platform here
                   pass  # Placeholder, replace this with actual logic and be sure to remove pass


               availability_text = f"{username} is {'available' if exists else 'taken'} on {selected_platform}.\n"
               availability_text += f"Profile link: {url}"
               availability_text_widget.delete(1.0, tk.END)
               availability_text_widget.insert(tk.END, availability_text)
           else:
               availability_text_widget.delete(1.0, tk.END)
               availability_text_widget.insert(tk.END, f"Failed to check {selected_platform} availability.")
       else:
           availability_text_widget.delete(1.0, tk.END)
           availability_text_widget.insert(tk.END, "Please select a valid platform.")
   else:
       availability_text_widget.delete(1.0, tk.END)
       availability_text_widget.insert(tk.END, "Please select a platform.")


def copy_url():
   url = availability_text_widget.get("2.0", "end-1c")  # Adjusted indexing
   if url:
       pyperclip.copy(url)
       messagebox.showinfo("Copied", "URL copied to clipboard.")


# Tkinter GUI setup
root = tk.Tk()
root.title("Username Availability Checker - The Pycodes")
root.geometry("450x300")


username_label = tk.Label(root, text="Enter username:")
username_label.grid(row=0, column=0)


username_entry = tk.Entry(root, width=23)  # Adjusted width
username_entry.grid(row=0, column=1)


platform_label = tk.Label(root, text="Select platform:")
platform_label.grid(row=1, column=0)


platform_combobox = ttk.Combobox(root, values=list(platforms.keys()))  # Dynamic platform options
platform_combobox.grid(row=1, column=1)


check_button = tk.Button(root, text="Check Availability", command=check_availability)
check_button.grid(row=2, column=0)


copy_button = tk.Button(root, text="Copy URL", command=copy_url)
copy_button.grid(row=2, column=1)


availability_text_widget = tk.Text(root, height=10, width=50)
availability_text_widget.grid(row=3, columnspan=2,padx=20)


root.mainloop()

Happy Coding!

Subscribe for Top Free Python Tutorials!

Receive the best directly.  Elevate Your Coding Journey!

Leave a Comment

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

Scroll to Top
×