DEV Community

Pratik Pathak
Pratik Pathak

Posted on • Originally published at pratikpathak.com on

The best way to get the Owner Name of a Number using Python WhatsApp in BULK!

Recently, I found myself in a situation where I had to find the name of a number, in layman’s terms, the owner name of a number. While going through a lot of methods reverse number lookup via Truecaller, Google search, Facebook Search, etc. Finally, I found a promising new method which uses WhatsApp. Sooner I realized it’s a very time-consuming method and out of all I realized I have to check around more than 50 thousand numbers.

What do we do when we do repetitive tasks? Yes, you guessed it correctly! Automation.

As a programmer, I started implementing a coding method to develop a tool that can do all these things.

How to find owner Name of a Number using python?

Though it seems to be difficult it’s quite easy. There are various methods you can achieve this, the most popular and easy way is by searching it on the Truecaller App. The only drawback here is you cannot do a bulk search.

Moving on there are also many online tools available but most of them are paid. While searching for a good way I realized WhatsApp also shows the name of the owner.

WhatsApp shows the owner name of a number if we just click on the profile and there it is. Voila, we got our name. So the logical steps are

  • Open the number on WhatsApp
  • Click on the WhatsApp profile
  • Check out the name

Here is a sample image where you can see the owner’s name:

Name of a Number using python




Name of a Number using python

Name of a Number using python
Name of a Number using python

Name of a Number using python
Name of a Number using python

Now the only thing remaining is to write a program, that will open the WhatsApp of all the numbers, check their name in the profile, and save it in a file.

Full project GitHub Link: Github Link

Requirements of Tool :

By analyzing it closely we can say that we are going to communicate with a browser and the most popular browser is Chrome.

In order to communicate with Browser we will need Selenium, and Python.

Let the Programming begin…

The first we need to do is import the required packages. the only third-party package we need is selenium. For the inbuilt, we will need OS, Time, Pathlib, and Shutil.

Selenium will be used to control the Chrome browser with Python, the OS module is for doing tasks at the OS level like checking whether a file exists or not, Pathlib to manage relative paths, and Shutil is used to delete folders.

So here are the header files that we need to import in order to make our program run.

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import pathlib
import shutil
Enter fullscreen mode Exit fullscreen mode

[Updated] Create Kali Linux Live USB with Persistence storage 2023

Now we will create a folder to save our data from Chrome and the next time we launch Chrome it should read it from here.

# create a folder name "ChromeData" in current directory
os.makedirs("ChromeData", exist_ok=True)
scriptDirectory = pathlib.Path().absolute()
Enter fullscreen mode Exit fullscreen mode

we are also saving the absolute path, which will help us to read and write in various files.

If you like our project don’t forget to star our repo Github Link

Login to WhatsApp using Python

Now we will log in to the Whatsapp account using the Python program. We are going to open WhatsApp web in Chrome browser and the user has to scan the QR code using “WhatsApp>Linked Devices > Link a Device” and it will get logged in, The User has to do it only one time.

To achieve the task we write

chrome_options = Options()

# add argument to chrome options object to save data in "ChromeData" folder
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--window-size=1200,800")
chrome_options.add_argument(f"user-data-dir={scriptDirectory}\\ChromeData")

# create a chrome driver object

driver = webdriver.Chrome(options=chrome_options)
# driver.maximize_window()
driver.get("https://web.whatsapp.com/")
time.sleep(20)

# create a file name "login.txt" in current directory
# and write "loged In" in it
# this file will be used to check if user is logged in or not
with open("login.txt", "w") as file:
    file.write("loged In")

Enter fullscreen mode Exit fullscreen mode

We are also creating a file named “login.txt” just to tell the program user has already logged in. The program will check the presence of the “login.txt” file, if it’s present it will skip the login part else it will prompt the user to log in.

What if something went wrong?

In the programming world always something goes wrong, like logging in but cookies expired or some unknown error occurring in the system, this leads to many issues in finding the name of a number, to tackle this issue we have to make the program reset itself and clear all the files related to chrome browser and also delete “login.txt” to tell the program it has to login back again.

We will create a function named “resetTool()” so that we can use this many times. resetTool() looks something like this…

def resetTool():
    # remove the folder "ChromeData" and all its content from current directory for both windows and linux
    shutil.rmtree("ChromeData", ignore_errors=True)

    # remove the file "login.txt" from current directory
    os.remove("login.txt")

    # restart the program
    os.system("python CheckNum.py")
Enter fullscreen mode Exit fullscreen mode

I’m using shutil instead of “os.remove()” to delete the whole folder and all it’s content and also to make it platform-independent so that it can easily run on both Windows and Linux operating systems. Since deleting a folder command is different in windows and linux

resetTool will also restart the Python program for checking the name of a number.

The heart of the Tool:

Now we are moving to the main part, where it will use selenium to find the name attached to a number in WhatsApp.

I have created a function named “findName()” to achieve this task. Here is the full code of findName…

def findName(num="", number=""):
    # create a chrome options object
    if num != "":
        chrome_options = Options()

        # add argument to chrome options object to save data in "ChromeData" folder
        # chrome_options.add_argument('--user-data-dir=ChromeData')

        chrome_options.add_argument("--disable-infobars")
        chrome_options.add_argument("--window-size=1200,800")
        chrome_options.add_argument(f"user-data-dir={scriptDirectory}\\ChromeData")

        # make chrome headless
        # chrome_options.add_argument("--headless")

        # create a chrome driver object
        driver = webdriver.Chrome(options=chrome_options)
        # driver.maximize_window()
        # driver.get("https://web.whatsapp.com/")
        # time.sleep(20)

        # open a new tab
        # driver.find_element("tag",'body').send_keys(Keys.CONTROL + 't') 
        driver.get("https://wa.me/+91"+num)


        try:
            # find element by xpath
            driver.find_element("xpath",'//*[@id="action-button"]/span').click()
            time.sleep(2)
            name = driver.find_element("xpath",'//*[@id="fallback_block"]/div/div/h4[2]/a/span').click()

            sendMsgBox = '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]/p'
            # wait until sendMsgBox is visible
            wait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, sendMsgBox)))

            driver.find_element("xpath",'//*[@id="main"]/header/div[2]/div[1]/div').click()
            time.sleep(2)


            # time.sleep(400)
            # print name
            print(name.text)
            # close chrome
            driver.close()
            return name.text
        except Exception as e:
            print(e)
            # print("Number not found")

        # close chrome
        driver.close()

Enter fullscreen mode Exit fullscreen mode

You can find the full code here: Github Link

The code is pretty self-explanatory, it does not have any complex code or logical block, it simply opens WhatsApp web, opens a user profile, checks the name of a number, and gives it back to you.

Conclusion

In the end, it’s a brute force method that is slow yet effective and much faster than humans. I wanted to make it multithreading to increase its speed but never got the time. If you want to contribute feel free to contribute to our repo.

The same thing can be achieved using other programming languages too like Java, NodeJS, etc. The logical part is the same only it will differ by implementation part.

Top comments (0)