通过淘宝的API "http://ip.taobao.com/service/getIpInfo.php?ip=*.*.*.*" 来获得你要查询的IP地址的国家,地区,城市,ISP等

地理位置信息.

  Python代码如下:

#!/usr/bin/python2.7
#File: IP2Region.py
#Author: lxw
#Time: 2014-12-03
#Usage: Get the location information from IP.
#Reference:http://smilejay.com/2014/05/python-get-geoinfo-from-ip/
#The data format that location_taobao.getLocation() returns is as follow:
#{
# "code":0,
# "data":{
# "country":"\u53f0\u6e7e",
# "country_id":"TW",
# "area":"",
# "area_id":"",
# "region":"\u53f0\u6e7e\u7701",
# "region_id":"TW_01",
# "city":"",
# "city_id":"",
# "county":"",
# "county_id":"",
# "isp":"",
# "isp_id":"-1",
# "ip":"218.211.14.165"
# }
#} import json
import urllib2 class location_taobao(object):
'''
Build the map of IP and its location.
The location info is from Taobao
e.g. http://ip.taobao.com/service/getIpInfo.php?ip=218.211.14.165
The getIpInfo API from Taobao returns a JSON object.
'''
def __init__(self, ip):
self.ip = ip
self.url = "http://ip.taobao.com/service/getIpInfo.php?ip={0}".format(self.ip)
#NOTE: OK.
#Call another method in the CONSTRUCTOR(__init__()).
self.location = self.getLocation() def getLocation(self):
'''
Get the location info from self.url.
The location info are involved in the JSON object returned.
'''
#"urlopen()" is like the built-in function "open()" for "file".
urlobj = urllib2.urlopen(self.url)
data = urlobj.read() #str
dataDict = json.loads(data, encoding="utf-8") # dict
return dataDict["data"] #The type of dataDict["data"] is still dict. def getCountry(self):
#self.location can be str("invalid Ip") or dict.
try:
result = self.location.get(u"country")
except:
pass
else:
return result def getRegion(self):
try:
result = self.location.get(u"region")
except:
pass
else:
return result def getCity(self):
try:
result = self.location.get(u"city")
except:
pass
else:
return result def getISP(self):
try:
result = self.location.get(u"isp")
except:
pass
else:
return result def main():
while 1:
ip = raw_input("Please input the IP you want to check(Input \"END\" to stop):\n")
if ip == "END":
break obj = location_taobao(ip) #print("Country: {0}".format(obj.getCountry())) #NOT OK. encode.
print(u"Country: {0}".format(obj.getCountry()))
print(u"Region: {0}".format(obj.getRegion()))
print(u"City: {0}".format(obj.getCity()))
print(u"ISP: {0}\n".format(obj.getISP())) if __name__ == '__main__':
main()
else:
print("Being imported as a module.")

Reference:

python根据IP查地理位置信息: http://smilejay.com/2014/05/python-get-geoinfo-from-ip/

Get Region Information from IP Address with Taobao API的更多相关文章

  1. Windows Azure Virtual Network (6) 设置Azure Virtual Machine固定公网IP (Virtual IP Address, VIP) (1)

    <Windows Azure Platform 系列文章目录> 注意:本文介绍的是Global Azure (http://www.windowsazure.com),如果你使用的是由世纪 ...

  2. Assign an Elastic IP Address to Your Instance

    By default, an instance in a nondefault VPC is not assigned a public IP address, and is private.You ...

  3. How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

    Question: On CentOS 7, I want to switch from DHCP to static IP address configuration with one of my ...

  4. 华东师大OJ:IP Address【IP地址转换】

    /*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...

  5. Linux Force DHCP Client (dhclient) to Renew IP Address

    http://www.cyberciti.biz/faq/howto-linux-renew-dhcp-client-ip-address/‘m using Ubuntu Linux. How to ...

  6. How to block a specific IP Address using UFW

    How to block a specific IP Address using UFW The key to blocking a specific IP address with UFW is t ...

  7. 如何在没有显示器的情况下,查看 Raspberry Pi 3的 IP 信息(Raspberry Pi 3 ,IP Address)

    1. 如何在没有显示器的情况下,查看 Raspberry Pi 3的 IP 信息(Raspberry Pi 3 ,IP Address) 1 IP Address Any device connect ...

  8. wifi IP address scanner on macOS

    wifi IP address scanner on macOS Nmap Network Scanning https://nmap.org/book/inst-macosx.html https: ...

  9. ERROR 2003 (HY000): Can't connect to MySQL server on 'ip address' (111)的处理办法

    远程连接mysql数据库时可以使用以下指令 mysql -h 192.168.1.104 -u root -p 如果是初次安装mysql,需要将所有/etc/mysql/内的所有配置文件的bind-a ...

随机推荐

  1. Atitit避免出现空指针异常解决方案

    Atitit避免出现空指针异常解决方案 1. Null的问题1 2. 强制区分一般引用vs 可空引用 vs 强制引用,或者说非空引用2 3. ?运算符(问号运算符) !感叹号运算符避免出现空指针异常, ...

  2. PLSQL配置数据库的方式

    1.直接连接的方式   2.修改客户端D:\app\Administrator\product\11.2.0\client_1\network\admin\tnsnames.ora文件的方式. ora ...

  3. 更改centos 7 的默认启动为命令界面

    vi /etc/inittab 可以看到这样两行: # To set a default target, run:# systemctl set-default TARGET.target 故: su ...

  4. 如何用MathType编辑圆圈符号

    数学往往是由很多的公式和符号组成的,一些用户朋友在写文章需要用到一些符号的时候,发现一般的编辑器很难满足一些比较专业的需求.这个时候就需要一款专业的数学公式编辑器.公式编辑器MathType就是这样诞 ...

  5. Windows中安装Scrapy

    在linux中安装Scrapy只需要导入一些非python的支持包,在windows中安装Scrapy则是一波三折. 总之来说,主要分为以下几个步骤,可能由于系统问题(国内个人机子,甚至是小企业的机子 ...

  6. 第四篇:了解 C++ 默默编写并调用的函数

    前言 对于一个类来说,最最基础的三类成员函数莫过于:构造函数,析构函数以及拷贝函数 (copy构造函数和=重载函数).即使你的类没有为这些函数做出定义,C++ 也会自动为你创建.本文将讲述的是 C++ ...

  7. <转> 堆和栈的区别

    一.预备知识—程序的内存分配    一个由C/C++编译的程序占用的内存分为以下几个部分    1.栈区(stack)—   由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数 ...

  8. 【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树

    [BZOJ4817][Sdoi2017]树点涂色 Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路 ...

  9. 【BZOJ3930】[CQOI2015]选数 莫比乌斯反演

    [BZOJ3930][CQOI2015]选数 Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律 ...

  10. Hints of sd0061(快排思想)

    Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...