本文实例讲述了Python2.7基于淘宝接口获取IP地址所在地理位置的方法。分享给大家供大家参考,具体如下:
#!/usr/bin/python import sys, os, urllib2, json def get_ip_area(ip): try: apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" %ip content = urllib2.urlopen(apiurl).read() data = json.loads(content)['data'] code = json.loads(content)['code'] if code == 0: # success print(data['country_id']) print(data['area']) print(data['city']) print(data['region']) else: print(data) except Exception as ex: print(ex) if __name__ == '__main__': ip = '123.125.114.144' #ip = 'www.baidu.com' # invalid ip. get_ip_area(ip)
运行效果图如下:
本文章地址http://www.vzeo.com/news/xuetang/800621.html 由 友站网 编辑整理,转载请注明出处