最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

這篇文章中的內(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.

復制代碼 代碼如下:

#!/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()

相關文章

  • Python?如何引用不確定的函數(shù)

    Python?如何引用不確定的函數(shù)

    在Python中,引用不確定的函數(shù)通常意味著我們可能在運行時才知道要調(diào)用哪個函數(shù),或者我們可能想根據(jù)某些條件動態(tài)地選擇不同的函數(shù)來執(zhí)行,下面給大家分享Python?如何引用不確定的函數(shù),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • python使用matplotlib顯示圖像失真的解決方案

    python使用matplotlib顯示圖像失真的解決方案

    這篇文章主要介紹了python使用matplotlib顯示圖像失真的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • python3+PyQt5自定義視圖詳解

    python3+PyQt5自定義視圖詳解

    這篇文章主要為大家詳細介紹了python3+PyQt5自定義視圖的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python制作七夕比心表白代碼詳解

    Python制作七夕比心表白代碼詳解

    在本篇文章里小編給大家整理的是一篇關于Python制作七夕比心表白代碼詳解內(nèi)容,有需要的朋友們可以學習參考下。
    2021-08-08
  • Python使用matplotlib繪制動畫的方法

    Python使用matplotlib繪制動畫的方法

    這篇文章主要介紹了Python使用matplotlib繪制動畫的方法,涉及matplotlib模塊的常見使用技巧,需要的朋友可以參考下
    2015-05-05
  • 用python的哈希函數(shù)對密碼加密

    用python的哈希函數(shù)對密碼加密

    大家好,本篇文章主要講的是用python的哈希函數(shù)對密碼加密,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • python re庫的正則表達式入門學習教程

    python re庫的正則表達式入門學習教程

    這篇文章主要給大家介紹了關于python re庫的正則表達式的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03
  • Python爬蟲防封ip的一些技巧

    Python爬蟲防封ip的一些技巧

    這篇文章主要介紹了Python爬蟲防封ip的一些技巧,對平時學習爬蟲有所幫助,感興趣的朋友可以了解下
    2020-08-08
  • Python 字符串、列表、元組的截取與切片操作示例

    Python 字符串、列表、元組的截取與切片操作示例

    這篇文章主要介紹了Python 字符串、列表、元組的截取與切片操作,結(jié)合實例形式分析了Python針對字符串、列表、元組的截取與切片相關操作技巧,需要的朋友可以參考下
    2019-09-09
  • PyQt5按下按鍵選擇文件夾并顯示的實現(xiàn)

    PyQt5按下按鍵選擇文件夾并顯示的實現(xiàn)

    這篇文章主要介紹了PyQt5按下按鍵選擇文件夾并顯示的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論

三穗县| 南部县| 垫江县| 尤溪县| 安阳县| 清远市| 青河县| 遵义市| 北川| 邓州市| 乐平市| 霍林郭勒市| 贵德县| 新化县| 仪征市| 呼图壁县| 澄迈县| 乾安县| 于都县| 商都县| 德昌县| 宣汉县| 望都县| 隆尧县| 上饶市| 武冈市| 墨竹工卡县| 吴江市| 全椒县| 荣昌县| 合阳县| 达拉特旗| 象州县| 高陵县| 揭西县| 民乐县| 雅安市| 莱西市| 临漳县| 天水市| 崇礼县|