Breaking News

Find IP Geolocation in Python



1 - Install BeautifulSoup: pip install beautifulsoup4
2 - Copy Paste Code and Run

Code

from urllib.request import urlopen
from bs4 import BeautifulSoup

# IDEA Developers
# @ideadevelopers

class IPLocationFinder:
    def __init__(self):
        self.keycdn = "https://tools.keycdn.com/geo?host="

    def findIpLocation(self, ipaddr):
        self.keycdn = self.keycdn + ipaddr
        html_page = urlopen(self.keycdn)
        soup = BeautifulSoup(html_page, 'html.parser')

        jsonData = soup.find("table").text.strip()
        jsonData = jsonData.splitlines()

        dataLength = len(jsonData ) -1;
        for x in range(0, dataLength, 2):
            if jsonData[x] and jsonData[ x +1]:
                data = jsonData[x] + " = " + jsonData[ x +1]
                print(data)


    def startApp(self, ipaddr):
        if not ipaddr:
            print("Please enter a valid ip address!")
        else:
            self.findIpLocation(ipaddr)


if __name__ == "__main__":
    ipLoc = IPLocationFinder()
    ipLoc.startApp("172.217.10.14")



No comments