【学习笔记】利用API进行数据采集或获取

利用百度普通IP定位API,坐标转换API进行IP定位

加载相关模块

import json
from urllib.request import urlopen

创建坐标列表,以便后续进行坐标提取

coords = []

创建坐标获取函数

def get_coordinate(ip_address):
	# 声明全局变量,以在函数中进行修改
    global coords
    # 利用ipstack提供的ip坐标查询API提取ip坐标数据
    # 语句.decode('utf-8')解码
    response = urlopen("http://api.ipstack.com/" + ip_address + 
    "?access_key=这里是我的key")\
        .read().decode('utf-8')
    # 载入json格式的数据
    response_json = json.loads(response)
    # 将获得ip地址经纬度加入到coords列表中
    coords.append(response_json.get("latitude"))
    coords.append(response_json.get("longitude"))
    # 分别返回ip地址的经纬度值
    return coords[0], coords[1]

创建坐标转换函数(转换为百度坐标系)

转换结果看貌似并没有表面上的区别,但为保证运行结果更准确,故而转换
def switch_coordinate():
	# 通过百度提供的API服务文档提取坐标转换数据
    response = urlopen("http://api.map.baidu.com/geoconv/v1/?coords=" + str(coords[1]) +
    	"," + str(coords[0]) + "&from=1&to=5&ak=这里是我的key").read().decode('utf-8')
  	response_json = json.loads(response)
  	# 返回转换结果
    return response_json.get("result")[0]['x'],\
           response_json.get("result")[0]['y']

创建ip地址获取函数

def get_location(ip_address):
	# 同样通过百度API服务文档提取相关数据
    response = urlopen("http://api.map.baidu.com/location/ip?ip=" + ip_address +
                       "&ak=这里是我的key&coor=" + str(switch_coordinate()[0]) +
                       "," + str(switch_coordinate()[1])).read().decode('utf-8')
    response_json = json.loads(response)
    address = response_json.get("content").get("address")
    city = response_json.get("content").get("address_detail")["city"]
    district = response_json.get("content").get("address_detail")["district"]
    province = response_json.get("content").get("address_detail")["province"]
    street = response_json.get("content").get("address_detail")["street"]
    street_number = response_json.get("content").get("address_detail")["street_number"]
    # 这里用个六个if判断语句,是为了让运行结果具有可读性,去掉无谓的标号
    if address != "":
        print("address:", address)
    if city != "":
        print("city:", city)
    if district != "":
        print("district", district)
    if province != "":
        print("province", province)
    if street != "":
        print("street", street)
    if street_number != "":
        print("street number:", street_number)

创建结果输出函数

def print_result(ip_address):
    print("原始坐标:", get_coordinate(ip_address))
    print("转换坐标:", switch_coordinate())
    print("\n————————Ip定位————————\n")
    get_location(ip_address)

运行函数

print_result(input("请输入IP地址:"))

完整代码如下:


import json
from urllib.request import urlopen
coords = []


def get_coordinate(ip_address):
    global coords
    response = urlopen("http://api.ipstack.com/" + ip_address + "?access_key=这里是我的key")\
        .read().decode('utf-8')
    response_json = json.loads(response)
    coords.append(response_json.get("latitude"))
    coords.append(response_json.get("longitude"))
    return coords[0], coords[1]


def switch_coordinate():
    response = urlopen("http://api.map.baidu.com/geoconv/v1/?coords=" + str(coords[1]) + "," + str(coords[0]) +
                       "&from=1&to=5&ak=这里是我的key").read().decode('utf-8')
    response_json = json.loads(response)
    return response_json.get("result")[0]['x'],\
           response_json.get("result")[0]['y']


def get_location(ip_address):
    response = urlopen("http://api.map.baidu.com/location/ip?ip=" + ip_address +
                       "&ak=这里是我的key&coor=" + str(switch_coordinate()[0]) +
                       "," + str(switch_coordinate()[1])).read().decode('utf-8')
    response_json = json.loads(response)
    address = response_json.get("content").get("address")
    city = response_json.get("content").get("address_detail")["city"]
    district = response_json.get("content").get("address_detail")["district"]
    province = response_json.get("content").get("address_detail")["province"]
    street = response_json.get("content").get("address_detail")["street"]
    street_number = response_json.get("content").get("address_detail")["street_number"]
    if address != "":
        print("address:", address)
    if city != "":
        print("city:", city)
    if district != "":
        print("district", district)
    if province != "":
        print("province", province)
    if street != "":
        print("street", street)
    if street_number != "":
        print("street number:", street_number)


def print_result(ip_address):
    print("原始坐标:", get_coordinate(ip_address))
    print("转换坐标:", switch_coordinate())
    print("\n————————Ip定位————————\n")
    get_location(ip_address)


print_result(input("请输入IP地址:"))

我们以网络随机选取几个ip地址为例

运行结果如下:

【学习笔记】利用API进行数据采集或获取

后续希望通过深入的学习,能在此基础上优化,更能利用百度或者Google或者其他地理信息API数据提供商,根据经纬度显示出确切的地理位置。

Thanks!