Skip to content

Phone

Developer docs

A class to represent a phone number

Source code in src/whitepyges/phone.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Phone:
    """
    A class to represent a phone number
    """

    def __init__(self, phone_number: str) -> None:
        """
        Initialize a new Phone object

        Args:
            phone_number (str): The phone number to search

        Returns:
            None
        """

        if not phone_number:
            raise ValueError("Phone number is required")

        self.phone_number = helper.format_phone_number(phone_number)

        self.headers = config.HEADERS

    def search(
        self,
        timeout: int = 10,
        max_retries: int = 3,
        randomize_headers: bool = False,
        ignore_robots: bool = False,
    ) -> dict | None:
        """
        Perform a search for the phone number

        Args:
            timeout (int, optional): The timeout for the request. Defaults to 10.
            max_retries (int, optional): The maximum number of retries. Defaults to 3.
            randomize_headers (bool, optional): Randomize the headers for the request. Defaults to False.
            ignore_robots (bool, optional): Ignore the robots.txt file. Defaults to False.

        Returns:
            list[dict] | None: Possible data for the phone number
        """

        endpoint: str = "number"

        url: str = helper.get_endpoint("phone", endpoint, number=self.phone_number)

        print(url)

        headers: dict = self.headers
        if randomize_headers:
            headers = helper.get_random_headers()

        response: requests.Response = helper.make_request_with_retries(
            url, headers, max_retries, timeout, ignore_robots
        )

        soup: BeautifulSoup = BeautifulSoup(response.text, "html.parser")

        phone_info_div = soup.find(
            "div", {"data-qa-selector": "phone-header-area-code-info"}
        )
        spam_info_div = soup.find("a", {"class": "wp-chip"})

        if not phone_info_div and not spam_info_div:
            return None

        splitting_info: tuple[str] = (
            "their location and provider",
            "Area code location",
            "Other major (",
            ") cities",
        )

        phone_info: str = phone_info_div.get_text(strip=True)

        for info in splitting_info:
            if info in phone_info:
                phone_info = phone_info.replace(info, f"|{info}|")

        phone_info_list: list[str] = phone_info.split("|")

        spam_info: str = "No spam info available"
        if spam_info_div:
            spam_info = spam_info_div.get_text(strip=True)

        state: str = "Unknown"
        cities: str = "Unkown"

        if "their location and provider" in phone_info_list:
            state = phone_info_list[phone_info_list.index("their location and provider") + 1]

        if "Area code location" in phone_info_list:
            cities = phone_info_list[phone_info_list.index("Area code location") + 1]

        formatted_phone_info: dict = {
            "spam_info": spam_info,
            "state": state,
            "cities": cities,
            "area_code": self.phone_number[2:5] if self.phone_number else "Unknown",
            "url": url,
        }

        return formatted_phone_info

    def __repr__(self) -> str:
        """
        Return the string representation of the object

        Returns:
            str: The string representation of the object
        """

        return helper.format_repr(self)

    def __str__(self) -> str:
        """
        Return the string representation of the object

        Returns:
            str: The string representation of the object
        """

        return helper.format_str(self)

__init__

Initialize a new Phone object

Parameters:

Name Type Description Default
phone_number str

The phone number to search

required

Returns:

Type Description
None

None

Source code in src/whitepyges/phone.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def __init__(self, phone_number: str) -> None:
    """
    Initialize a new Phone object

    Args:
        phone_number (str): The phone number to search

    Returns:
        None
    """

    if not phone_number:
        raise ValueError("Phone number is required")

    self.phone_number = helper.format_phone_number(phone_number)

    self.headers = config.HEADERS

__repr__

Return the string representation of the object

Returns:

Name Type Description
str str

The string representation of the object

Source code in src/whitepyges/phone.py
113
114
115
116
117
118
119
120
121
def __repr__(self) -> str:
    """
    Return the string representation of the object

    Returns:
        str: The string representation of the object
    """

    return helper.format_repr(self)

__str__

Return the string representation of the object

Returns:

Name Type Description
str str

The string representation of the object

Source code in src/whitepyges/phone.py
123
124
125
126
127
128
129
130
131
def __str__(self) -> str:
    """
    Return the string representation of the object

    Returns:
        str: The string representation of the object
    """

    return helper.format_str(self)

search

Perform a search for the phone number

Parameters:

Name Type Description Default
timeout int

The timeout for the request. Defaults to 10.

10
max_retries int

The maximum number of retries. Defaults to 3.

3
randomize_headers bool

Randomize the headers for the request. Defaults to False.

False
ignore_robots bool

Ignore the robots.txt file. Defaults to False.

False

Returns:

Type Description
dict | None

list[dict] | None: Possible data for the phone number

Source code in src/whitepyges/phone.py
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def search(
    self,
    timeout: int = 10,
    max_retries: int = 3,
    randomize_headers: bool = False,
    ignore_robots: bool = False,
) -> dict | None:
    """
    Perform a search for the phone number

    Args:
        timeout (int, optional): The timeout for the request. Defaults to 10.
        max_retries (int, optional): The maximum number of retries. Defaults to 3.
        randomize_headers (bool, optional): Randomize the headers for the request. Defaults to False.
        ignore_robots (bool, optional): Ignore the robots.txt file. Defaults to False.

    Returns:
        list[dict] | None: Possible data for the phone number
    """

    endpoint: str = "number"

    url: str = helper.get_endpoint("phone", endpoint, number=self.phone_number)

    print(url)

    headers: dict = self.headers
    if randomize_headers:
        headers = helper.get_random_headers()

    response: requests.Response = helper.make_request_with_retries(
        url, headers, max_retries, timeout, ignore_robots
    )

    soup: BeautifulSoup = BeautifulSoup(response.text, "html.parser")

    phone_info_div = soup.find(
        "div", {"data-qa-selector": "phone-header-area-code-info"}
    )
    spam_info_div = soup.find("a", {"class": "wp-chip"})

    if not phone_info_div and not spam_info_div:
        return None

    splitting_info: tuple[str] = (
        "their location and provider",
        "Area code location",
        "Other major (",
        ") cities",
    )

    phone_info: str = phone_info_div.get_text(strip=True)

    for info in splitting_info:
        if info in phone_info:
            phone_info = phone_info.replace(info, f"|{info}|")

    phone_info_list: list[str] = phone_info.split("|")

    spam_info: str = "No spam info available"
    if spam_info_div:
        spam_info = spam_info_div.get_text(strip=True)

    state: str = "Unknown"
    cities: str = "Unkown"

    if "their location and provider" in phone_info_list:
        state = phone_info_list[phone_info_list.index("their location and provider") + 1]

    if "Area code location" in phone_info_list:
        cities = phone_info_list[phone_info_list.index("Area code location") + 1]

    formatted_phone_info: dict = {
        "spam_info": spam_info,
        "state": state,
        "cities": cities,
        "area_code": self.phone_number[2:5] if self.phone_number else "Unknown",
        "url": url,
    }

    return formatted_phone_info

Features

  • Lookup phone numbers to retrieve spam risk, state, cities, area code, and profile URL
  • Handles request retries and optional header randomization

Example Usage

from whitepyges import Phone

phone: Phone = Phone(phone_number='123-456-7890')
info: dict = phone.search()

print(info)

Example Response

{
    "spam_info": "LOW SPAM RISK",
    "state": "Washington",
    "cities": "Example, Example-2, Examples 3",
    "area_code": "123",
    "url": "https://www.whitepages.com/phone/1-123-456-7890"
}