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 ...
随机推荐
- Atitit.软件开发的非功能性需求attilax 总结At
Atitit.软件开发的非功能性需求attilax 总结 1. 运行环境约束:用户对软件系统运行环境的要求. 1 2. 兼容性 2 3. 7.6 数据库 database (imp by ati) ...
- 关于Xilinx FPGA JTAG下载时菊花链路中的芯片数量
关于Xilinx FPGA JTAG下载时菊花链路中的芯片数量 emesjx | 2014-08-13 13:13:30 阅读:1793 发布文章 当一个系统中含有多片(2片以上)Xil ...
- 设计模式之里氏替换原则(LSP)
在java等面向对象编程语言里面,我想继承性应该是一大特色吧!所以今天所要讲解的里氏替换原则主要是针对这一特性而提出来的,当我们定义对象的时候,尽量找出对象之间的相同点,然后将其抽象成基类对象.比如水 ...
- 初识Quartz(二)
简单作业: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package quartz_pr ...
- java-TransDemo
字符串转换成其他基本类型,以及包装类的自动装箱.拆箱 package com.example; import java.util.Scanner; import java.lang.*; /** * ...
- StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程【转载】
StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程[转] 声明:本文章是为了后期快速兼容6和6Plus的按比例放大方法,对于部分读者来说可能觉得该 ...
- mysql 索引优化,索引建立原则和不走索引的原因
第一:选择唯一性索引 唯一性索引的值是唯一的,可以更快捷的通过该索引来确定某条记录. 2.索引的列为where 后面经常作为条件的字段建立索引 如果某个字段经常作为查询条件,而且又有较少的重复列或者是 ...
- 关于搭建HTTPS服务器服务
关于 HTTPS 的基本原理大家都已经不再陌生,今天和大家说说如何搭建一个支持 HTTPS 的服务端. 服务端的 HTTPS HTTPS 已经几乎成为了当前互联网推荐的通信方式,它能最大化保证信息传输 ...
- 【转】支付宝WAP支付接口开发
支付宝WAP支付接口开发 因项目需要,要增加支付宝手机网站支付功能,找了支付宝的样例代码和接口说明,折腾两天搞定,谨以此文作为这两天摸索的总结.由于公司有自己的支付接口,并不直接使用这个接口,所以晚些 ...
- WCF服务寄宿IIS与Windows服务
WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的 ...