Get Region Information from IP Address with Taobao API
通过淘宝的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的更多相关文章
- Windows Azure Virtual Network (6) 设置Azure Virtual Machine固定公网IP (Virtual IP Address, VIP) (1)
<Windows Azure Platform 系列文章目录> 注意:本文介绍的是Global Azure (http://www.windowsazure.com),如果你使用的是由世纪 ...
- 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 ...
- 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 ...
- 华东师大OJ:IP Address【IP地址转换】
/*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...
- 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 ...
- 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 ...
- 如何在没有显示器的情况下,查看 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 ...
- wifi IP address scanner on macOS
wifi IP address scanner on macOS Nmap Network Scanning https://nmap.org/book/inst-macosx.html https: ...
- 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 ...
随机推荐
- Apache配置文件详解
1.1 ServerRoot 配置 [ServerRoot "" 主要用于指定Apache的安装路径,此选项参数值在安装Apache时系统会自动把Apache的路径写入.Windo ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- placehoder修改
<textarea class="layui-input description" name="description" lay-verify=" ...
- codeblocks如何watch数组
codeblocks13.12+GDB 调试的时候,main传了一个int a[10]给quicksort 但是在quicksort内部,debugger把a看成一个pointer而不是array,所 ...
- is char signed or unsigned?
工作这么多年,一直认为char是有符号的,而事实上gcc和vs默认也是有符号,但是c规范里实际并没有指明char是有符号还是无符号,所以char比较特殊, char signed char unsig ...
- java -- 路径中包含空格怎么处理
@.使用toURI()方法 String rootPath = this.getClass().getClassLoader().getResource(".").toURI(). ...
- Struts2 是什么?
Struts2是流行和成熟的基于MVC设计模式的Web应用程序框架. Struts2不只是Struts1下一个版本,它是一个完全重写的Struts架构. WebWork框架开始以Struts框架为基础 ...
- linux各种路径小结
man路径 /etc/manpath.config $MANPATH include路径 $C_INCLUDE_PATH $CPLUS_INCLUDE_PATH 动态链接库的路径 $LD_LIBRAR ...
- go语言递归创建目录
import ( "fmt" "os" ) func main() { //创建C:/temp/log文件夹 // err := os.MkdirAll(&qu ...
- ACM至大二省赛小结
大一进acm坑的,大一上就学了个c,下才学c++,不过 c 学完后学 c++ 感觉很简单,应该是大一下开学左右才开始刷题的,前面都在水???然后因为acm协会有各种月赛校赛什么的,班主任的提醒较多,所 ...