欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用在線API查詢IP對應(yīng)的地理位置信息實例

 更新時間:2014年06月01日 22:29:05   作者:  
這篇文章主要介紹了python使用在線API查詢IP對應(yīng)的地理位置信息實例,需要的朋友可以參考下

這篇文章中的內(nèi)容是來源于去年我用美國的VPS搭建博客的初始階段,那是有很多惡意訪問,我就根據(jù)access log中的源IP來進行了很多統(tǒng)計,同時我也將訪問量最高的惡意訪問的源IP拿來查詢其地理位置信息。所以,我就用到了根據(jù)IP查詢地理位置信息的一些東西,現(xiàn)在將這方面積累的一點東西共享出來。

根據(jù)IP查詢所在地、運營商等信息的一些API如下(根據(jù)我有限的一點經(jīng)驗):
1. 淘寶的API(推薦):http://ip.taobao.com/service/getIpInfo.php?ip=110.84.0.129
2. 國外freegeoip.net(推薦):http://freegeoip.net/json/110.84.0.129 這個還提供了經(jīng)緯度信息(但不一定準)
3. 新浪的API:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=110.84.0.129
4. 騰訊的網(wǎng)頁查詢:http://ip.qq.com/cgi-bin/searchip?searchip1=110.84.0.129
5. ip.cn的網(wǎng)頁:http://www.ip.cn/index.php?ip=110.84.0.129
6. ip-api.com: http://ip-api.com/json/110.84.0.129 (看起來挺不錯的,貌似直接返回中文城市信息,文檔在 ip-api.com/docs/api:json)
7. http://www.locatorhq.com/ip-to-location-api/documentation.php (這個要注冊才能使用,還沒用過呢)

(第2個freegeoip.net的網(wǎng)站和IP數(shù)據(jù)的生成,代碼在:https://github.com/fiorix/freegeoip)

為什么其中第4、5兩個是網(wǎng)頁查詢也推薦了呢?是因為兩方面原因,一是它們提供的信息比較準,二是使用了頁面信息自動抓?。赡軙玫轿以?jīng)寫過的PhantomJS)也容易將其寫到程序中成為API。

根據(jù)IP查詢地理位置信息,我將其寫成了一個較為通用的Python庫(提供了前面提到的1、2、4、5等4種查詢方式的API),可以根據(jù)IP查詢到地域信息和ISP信息,具體代碼見:
https://github.com/smilejay/python/blob/master/py2013/iplocation.py
注意其中對ip.cn網(wǎng)頁的解析用到了webdriver和PhantomJS.

復(fù)制代碼 代碼如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on Oct 20, 2013
@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所在地為:<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('來自:')[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 = '110.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()

相關(guān)文章

  • minconda安裝pytorch的詳細方法

    minconda安裝pytorch的詳細方法

    這篇文章主要介紹了minconda安裝pytorch的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • python?numpy庫介紹

    python?numpy庫介紹

    這篇文章主要介紹了python?numpy庫,numpy是一個開源的python科學(xué)計算擴展庫,主要用來處理任意維度數(shù)組和矩陣。相同的任務(wù),使用numpy比直接用python的基本數(shù)據(jù)結(jié)構(gòu)更加簡單高效,下面一起進入文章了解更多詳細內(nèi)容吧
    2021-12-12
  • python人工智能tensorflow函數(shù)tf.get_variable使用方法

    python人工智能tensorflow函數(shù)tf.get_variable使用方法

    這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.get_variable使用方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Python命令行定時任務(wù)自動化工作流程

    Python命令行定時任務(wù)自動化工作流程

    本文介紹如何使用Python編寫定時任務(wù),以自動執(zhí)行命令行任務(wù)。您將學(xué)習(xí)如何安排定期的任務(wù),處理任務(wù)結(jié)果,以及如何使用Python自動化工作流程,從而提高工作效率。無需手動執(zhí)行重復(fù)任務(wù),Python幫您搞定
    2023-04-04
  • python3 map函數(shù)和filter函數(shù)詳解

    python3 map函數(shù)和filter函數(shù)詳解

    這篇文章主要介紹了python3 map函數(shù)和filter函數(shù)詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Python實現(xiàn)為圖片添加水印的示例詳解

    Python實現(xiàn)為圖片添加水印的示例詳解

    這篇文章主要介紹了如何通過Python3實現(xiàn)添加水印,這樣發(fā)朋友圈,圖片再也不怕被盜了!??!文中的示例代碼簡潔易懂,需要的可以參考一下
    2022-02-02
  • Python collections.defaultdict模塊用法詳解

    Python collections.defaultdict模塊用法詳解

    這篇文章主要介紹了Python collections.defaultdict模塊用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Python調(diào)用工具包實現(xiàn)發(fā)送郵件服務(wù)

    Python調(diào)用工具包實現(xiàn)發(fā)送郵件服務(wù)

    這篇文章主要為大家詳細介紹了Python圖畫調(diào)用工具包實現(xiàn)發(fā)送郵件服務(wù)的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-05-05
  • 詳解opencv Python特征檢測及K-最近鄰匹配

    詳解opencv Python特征檢測及K-最近鄰匹配

    這篇文章主要介紹了詳解opencv Python特征檢測及K-最近鄰匹配,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Python完成哈夫曼樹編碼過程及原理詳解

    Python完成哈夫曼樹編碼過程及原理詳解

    這篇文章主要介紹了Python完成哈夫曼樹編碼過程及原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07

最新評論