Tutorial on querying geographic location information based on IP address in python development

Source: Internet
Author: User
Tags documentation dcap in python

Because my website needs to query the source IP address to query its geographical location information. Therefore, I used some things to query geographical location information based on IP addresses, and now I have shared some accumulated things in this area.
First of all, when I first learned Python a few years ago, I have already written a similar blog (but it is not comprehensive enough). For details, see Python to get local IP addresses and locations.

Some APIs for querying the location and operator information based on the IP address are as follows (based on my limited experience ):
1. Taobao API (recommended): http://ip.taobao.com/service/getIpInfo.php? Ip = 110.84.0.129
2. freegeoip.net (recommended): http://freegeoip.net/json/110.84.0.129, which also provides latitude and longitude information (but not necessarily accurate)
3. Sina API: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json & ip = 110.84.0.129
4. Tencent web page query: http://ip.qq.com/cgi-bin/searchip? Searchip1 = 110.84.0.129
5. ip.cn webpage: http://www.ip.cn/index.php? Ip = 110.84.0.129
6. Ip-api.com: http://ip-api.com/json/110.84.0.129 (looks pretty good, it looks like a direct return of Chinese city information, documentation in ip-api.com/docs/api:json)
7. Http://www.locatorhq.com/ip-to-location-api/documentation.php (this need to register to use, haven't used it)

(2nd freegeoip.net website and IP data generation, code in: https://github.com/fiorix/freegeoip)

Why are two of them recommended for Web queries? The two reasons are as follows: first, they provide relatively accurate information, and second, they use automatic page information capture (may use PhantomJS I have previously written) it is also easy to write it into the program as an API.

Based on the IP address to query the geographic location information, I wrote it into a more common Python library (provides the APIs for the preceding four query methods: 1, 2, 4, and 5 ), you can query the region and ISP information based on the IP address. For the specific code, see:
Https://github.com/smilejay/python/blob/master/py2013/iplocation.py
Note that webdriver and PhantomJS are used for parsing ip.cn web pages.

The code is as follows: Copy code
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
 
'''
Created on Oct 20,201 3
@ Summary: geography info about an IP address
@ Author: Jay <smile665@gmail.com> http://smilejay.com/
'''
 
Import json, urllib2
Import re
From selenium import webdriver
From selenium. webdriver. common. desired_capabilities import DesiredCapabilities
 
 
Class location_freegeoip ():
'''
Build the mapping of the ip address and its location.
The geo info is from <freegeoip.net>
'''
 
Def _ init _ (self, ip ):
'''
Constructor of location_freegeoip class
'''
Self. ip = ip
Self. api_format = 'json'
Self. api_url = 'http: // freegeoip.net/%s/%s' % (self. api_format, self. ip)
 
Def get_geoinfo (self ):
"Get the geo info from the remote API.
Return a dict about the location.
"""
Urlobj = urllib2.urlopen (self. api_url)
Data = urlobj. read ()
Datadict = json. loads (data, encoding = 'utf-8 ')
# Print datadict
Return datadict
 
Def get_country (self ):
Key = 'country _ name'
Datadict = self. get_geoinfo ()
Return datadict [key]
 
Def get_region (self ):
Key = 'region _ name'
Datadict = self. get_geoinfo ()
Return datadict [key]
 
Def get_city (self ):
Key = 'city'
Datadict = self. get_geoinfo ()
Return datadict [key]
 
Class location_taobao ():
'''
Build the mapping of the ip address and its location
The geo info is from Taobao
E.g. Http://ip.taobao.com/service/getIpInfo.php? Ip = 112.111.184.63
The getIpInfo API from Taobao returns a JSON object.
'''
Def _ init _ (self, ip ):
Self. ip = ip
Self. api_url = 'http: // ip.taobao.com/service/getIpInfo.php? Ip = % s' % self. ip
 
Def get_geoinfo (self ):
"Get the geo info from the remote API.
Return a dict about the location.
"""
Urlobj = urllib2.urlopen (self. api_url)
Data = urlobj. read ()
Datadict = json. loads (data, encoding = 'utf-8 ')
# Print datadict
Return datadict ['data']
 
Def get_country (self ):
Key = u'country'
Datadict = self. get_geoinfo ()
Return datadict [key]
 
Def get_region (self ):
Key = 'region'
Datadict = self. get_geoinfo ()
Return datadict [key]
 
Def get_city (self ):
Key = 'city'
Datadict = self. get_geoinfo ()
Return datadict [key]
 
Def get_isp (self ):
Key = 'isp'
Datadict = self. get_geoinfo ()
Return datadict [key]
 
 
Class location_qq ():
'''
Build the mapping of the ip address and its location.
The geo info is from Tencent.
Note: the content of the Tencent's API return page is encoded by 'gb2312 '.
E.g. Http://ip.qq.com/cgi-bin/searchip? Searchip1 = 112.111.184.64
'''
Def _ init _ (self, ip ):
'''
Construction of location_ipdotcn class.
'''
Self. ip = ip
Self. api_url = 'http: // ip.qq.com/cgi-bin/searchip? Searchip1 = % s' % ip
 
Def get_geoinfo (self ):
Urlobj = urllib2.urlopen (self. api_url)
Data = urlobj. read (). decode ('gb2312'). encode ('utf8 ')
Pattern = re. compile (r'ip address location: <span> (. +) </span> ')
M = re. search (pattern, data)
If m! = None:
Return m. group (1). split ('& nbsp ;')
Else:
Return None
 
Def get_region (self ):
Return self. get_geoinfo () [0]
 
Def get_isp (self ):
Return self. get_geoinfo () [1]
 
 
Class location_ipdotcn ():
'''
Build the mapping of the ip address and its location.
The geo info is from www.ip.cn
Need to use PhantomJS to open the URL to render its JS
'''
Def _ init _ (self, ip ):
'''
Construction of location_ipdotcn class.
'''
Self. ip = ip
Self. api_url = 'http: // www.ip.cn/s' % ip
 
Def get_geoinfo (self ):
Dcap = dict (DesiredCapabilities. PHANTOMJS)
Dcap ["phantomjs. page. settings. userAgent"] = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv: 25.0) Gecko/20100101 Firefox/29.0 ")
Driver = webdriver. PhantomJS (executable_path = '/usr/local/bin/phantomjs', desired_capabilities = dcap)
Driver. get (self. api_url)
Text = driver. find_element_by_xpath ('// div [@ id = "result"]/div/P'). text
Res = text. split ('From:') [1]. split ('')
Driver. quit ()
Return res
 
Def get_region (self ):
Return self. get_geoinfo () [0]
 
Def get_isp (self ):
Return self. get_geoinfo () [1]
 
 
If _ name _ = '_ main __':
Ip = '1970. 84.0.129'
# Iploc = location_taobao (ip)
# Print iploc. get_geoinfo ()
# Print iploc. get_country ()
# Print iploc. get_region ()
# Print iploc. get_city ()
# Print iploc. get_isp ()
# Iploc = location_qq (ip)
Iploc = location_ipdotcn (ip)
# Iploc. get_geoinfo ()
Print iploc. get_region ()
Print iploc. get_isp ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.