【Python】利用百度API获取IP精确地址

# -*- coding:utf-8 -*-
import sys
import urllib2
import json
ak='Your baidu AK'
#http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip
def get_ip_information(ip):
url='http://api.map.baidu.com/highacciploc/v1?qcip='+ip+'&qterm=pc&ak='+ak+'&coord=bd09ll&extensions=3'
poiss=''
request = urllib2.Request(url)
page = urllib2.urlopen(request, timeout=10)
data_json = page.read()
data_dic = json.loads(data_json)
if(data_dic.has_key("content")):
content=data_dic["content"]
address_component=content["address_component"]
formatted_address=content["formatted_address"]
print "该IP地址的具体位置为:"
print address_component["country"]
print formatted_address
if (content.has_key("pois")):
print "该IP地址附近POI信息如下:"
pois = content["pois"]
for index in range(len(pois)):
pois_name = pois[index]["name"]
pois_address = pois[index]["address"]
print pois_name, pois_address
else:
print 'IP地址定位失败!!!'
if __name__ == '__main__':
get_ip_information('14.215.177.37')