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 ...
随机推荐
- 恩布企业 IM iOS端 1.1 公布, 开源手机 IM
恩布企业IM的 iOS 苹果开源手机client EntboostChat 公布 1.1 开发版本号.开源企业IM.企业即时通讯软件: 主要更新内容: 录界面添加自己定义LOGO显示 持部门.群组.联 ...
- iOS开发多线程篇 11 —自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
- c++ 宏 #val 在unicode下的使用。
#define CHECK(condition) cout<<check failed:<<#condition<<endl; 上面这句宏,当你 CHECK(myf ...
- webdriver下拉框中选择option的方法提醒
select这个标签比较特殊 下面的option不能用点击下拉框,再点击选中这种方法 前端代码: <html> <body> <select id="Shipp ...
- CentOS卸载系统自带的OpenJDK
查看目前系统的jdk: rpm -qa | grep jdk 得到的结果: $ rpm -qa | grep jdk java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el ...
- IOS设计模式的六大设计原则之依赖倒置原则(DIP,Dependence Inversion Principle)
定义 高层模块不应该依赖于低层模块,二者都应该依赖于抽象:抽象不应该依赖细节:细节应该依赖抽象. 定义解读 依赖倒置原则在程序编码中经常运用,其核心思想就是面向接口编程,高层模块不应该依赖低层模块(原 ...
- fzu 2250 不可能弹幕结界 分析+模拟,考察思维严谨。
Problem 2250 不可能弹幕结界 Accept: 5 Submit: 13Time Limit: 1000 mSec Memory Limit : 65536 KB Problem ...
- C# - Passing Reference-Type Parameters
A variable of a reference type does not contain its data directly; it contains a reference to its da ...
- samba在linux下的配置
Samba配置过程容易遇到的问题 samba的安装很简单,搜索一下都能找到.我主要是记录一上我碰到的问题及解决的办法 第一个问题 防火墙的问题,粗暴的办法是直接把防火墙关掉,合适的做法是把samba用 ...
- 第一百九十七节,jQuery EasyUI,LinkButton(按钮)组件
jQuery EasyUI,LinkButton(按钮)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 LinkButton(按钮)组件的使用方法,这个组 ...