Global phone carrier lookup using python

5.2k Views Asked by At

I got this code from pypi:

from phonenumbers import carrier

ro_number = phonenumbers.parse("+40721234567", "RO")

carrier.name_for_number(ro_number, "en")

Output:

'Vodafone'

I am in India. When I write this code for my phone number it works perfectly and shows Airtel. But my project is on finding the carrier of USA phone number +1xxx xxx xxx. When I do this for a USA number it returns a null string because mapping is not done.

Please help me to do this for US numbers.

Should I change my laptop location? I don't know how to do.

Or should I add pycountry?

Please help me.

1

There are 1 best solutions below

2
Tabulate On

Im a little late to this but I found a solution. phonenumbers does not work for US phone numbers. I have written a little script to get carriers from US phone numbers for free. Here it is:

import requests
import json
def getCarrier(number):
    url = 'https://api.telnyx.com/v1/phone_number/1' + number
    html = requests.get(url).text
    data = json.loads(html)
    carrier = data["carrier"]["name"]
    return carrier

All it does is scrape some json from a URL and returns the carrier. Hope someone finds this useful!