python3.4學習筆記(二十三) Python調用淘寶IP庫獲取IP歸屬地返回省市運營商實例代碼
淘寶IP地址庫 http://ip.taobao.com/
目前提供的服務包括:
1. 根據用戶提供的IP地址,快速查詢出該IP地址所在的地理信息和地理相關的信息,包括國家、省、市和運營商。
2. 用戶可以根據自己所在的位置和使用的IP地址更新我們的服務內容。
我們的優勢:
1. 提供國家、省、市、縣、運營商全方位信息,信息維度廣,格式規范。
2. 提供完善的統計分析報表,省准確度超過99.8%,市准確度超過96.8%,數據質量有保障。
=======================================
1 #!/usr/local/bin/python 2 #coding: utf-8 3 __author__ = 'zdz8207' 4 import json 5 import urllib 6 import sys 7 8 def get_data(ip): 9 url = "http://ip.taobao.com/service/getIpInfo.php?ip="+ ip 10 jsondata = json.loads(urllib.urlopen(url).read()) 11 #{u'code': 0, u'data': {u'ip': u'119.124.101.221', u'city': 12 #其中code的值的含義為,0:成功,1:失敗。{u'code': 1, u'data': u'invaild ip.'} 13 #print(jsondata) 14 if jsondata['code'] == 1: 15 jsondata['data'] = {'region':'','city':'','isp':''} 16 return (jsondata['data']['region'], jsondata['data']['city'], jsondata['data']['isp']) 17 18 if __name__ == "__main__": 19 #211.162.62.161 61.135.157.156 220.198.192.0 119.124.101.221 20 result = get_data("211.162.62.161") 21 print(result[0]+result[1]+result[2])
======================================
python中數字和字符串連接的方法:print str(x) + " is a string"
======================================
Python獲取IP歸屬地(修改版) - 開源中國社區
http://www.oschina.net/code/snippet_995588_18859
淘寶IP庫API地址:http://ip.taobao.com/service/getIpInfo.php?ip=xxx
用python的tkinter模塊寫了個查詢IP地址的小程序 - 推酷
http://www.tuicool.com/articles/nQjMNb7
./queryip.py 61.135.157.156
country area region city county isp
中國 華北 北京市 北京市 NULL 聯通
======================================
python通過淘寶IP地址庫獲取IP位置
淘寶IP地址庫提供了兩大服務:
1. 根據用戶提供的IP地址,快速查詢出該IP地址所在的地理信息和地理相關的信息,包括國家、省、市和運營商。
2. 用戶可以根據自己所在的位置和使用的IP地址更新我們的服務內容。
優勢:
1. 提供國家、省、市、縣、運營商全方位信息,信息維度廣,格式規范。
2. 提供完善的統計分析報表,省准確度超過99.8%,市准確度超過96.8%,數據質量有保障。
這個服務也是限制每個用戶的訪問頻率需小於10qps
整個流程分兩步:一,構造網址並讀取網頁數據;二,json解析網頁數據。
代碼如下:
import urllib, urllib2 import json import sys import re ip = '61.135.157.156'#raw_input(r'IP : ') ip = str.strip(ip) ptn = re.compile(r'(([12][0-9][0-9]|[1-9][0-9]|[1-9])\.){3,3}([12][0-9][0-9]|[1-9][0-9]|[1-9])') rel = ptn.match(ip) if rel: pass else: print "IP not valid" sys.exit() try: urlfp = urllib.urlopen('http://ip.taobao.com/service/getIpInfo.php?ip='+ ip) except Exception, e: print "Error ", e sys.exit() ipdata = urlfp.read() urlfp.close() allinfo = json.loads(ipdata) for oneinfo in allinfo: if "code" == oneinfo: if 0 == allinfo[oneinfo]: print "ip : " + allinfo["data"]["ip"] print "city : " + allinfo["data"]["country"], print allinfo["data"]["region"], print allinfo["data"]["city"], print "(" + allinfo["data"]["isp"] + ")" else: print "parse error" sys.exit()