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 ...
随机推荐
- YUYV格式到RGB格式的转换
为什么YUYV格式要转到RGB格式,视频的显示调用的多数API都是基于RGB格式,所以需要进行格式的转换. YUYV格式如下: Y0U0Y1V0 Y2U1Y3V1.......... 说明:一个Y代表 ...
- Tomcat的类加载器
看完了Java类装载器,我们再来看看应用服务器(Tomcat)对类加载器的使用,每个应用服务器都有一套自己的类加载器体系,从而与Java的类加载器区别开以达到自己与应用程序隔离的目的.Tomcat的类 ...
- Python内置函数之chr()
参数是一个整型对象: chr(integer) 返回整型对应的Unicode字符对象. 例子: >>> chr() '\x0c' >>> chr() 'ⲓ' > ...
- java - day14 - InnerClass
内部类使用 package com.InnerClass; public class Mama { String name; Baby baby; Mama(String name){ this.na ...
- mysql之log-slave-updates参数
1.引言 使用Mysql的replication机制实现主从同步时,其是由三个线程实现了,主库一个I/O线程,从库一个I/O线程和一个SQL线程.配置时主库需要开始bin-log参数,即在配置文件中添 ...
- IOS设计模式的六大设计原则之依赖倒置原则(DIP,Dependence Inversion Principle)
定义 高层模块不应该依赖于低层模块,二者都应该依赖于抽象:抽象不应该依赖细节:细节应该依赖抽象. 定义解读 依赖倒置原则在程序编码中经常运用,其核心思想就是面向接口编程,高层模块不应该依赖低层模块(原 ...
- Hibernate通过one-to-one元素的一对一映射
正如我们在前面的例子中讨论过的,在hibernate中执行一对一映射有两种方法: 通过many-to-one元素 通过one-to-one元素 这里,我们将通过one-to-one元素进行一对一的映射 ...
- Eclipse 关闭项目
Eclipse 关闭项目 为什么要关闭项目? Eclipse 工作空间包含了多个项目.一个项目可以是关闭或开启状态. 项目打开过多影响有: 消耗内存 占用编译时间:在删除项目.class 文件(Cle ...
- Vmware私有云虚拟机(CentOS 6.5 OS)之根分区扩容
注:适用于未使用lvm管理的分区,目前仅在CentOS 6.5 上操作,其他系统尚未测试,请谨慎操作 一.查看当前分区状况 [root@disk-test ~]# df -h Filesystem ...
- Week 3: Assessing performance 笔记
得到一个模型之后如何评价其性能? training error & generalization error & test error 如何理解generalization error ...