IP查詢的即用API,能夠查詢到要要查找IP的一些基本信息。html
示例代碼:python
#-*- coding: utf-8 -*- #version:0.1 #note:該即用API能查詢IP的地址(國家——省份——城市——地點——運營商) import urllib.request import json import collections url = "http://apistore.baidu.com/microservice/iplookup?ip=" ip = input("輸入你想查詢的IP地址:") url = url + ip #完整的URL result = urllib.request.urlopen(url).read().decode("utf-8") info = json.loads(result,object_pairs_hook=collections.OrderedDict) #json格式轉換爲python格式,並指定爲有序字典 if (info['errNum'] == -1): #查找失敗 print(info['errMsg']) else: #輸出天氣相關信息 print("你查詢的IP地址信息以下:") print("IP:", info['retData']['ip']) print("國家:", info['retData']['country']) print("省份:", info['retData']['province']) print("城市:", info['retData']['city']) print("地點:", info['retData']['district']) print("運營商:", info['retData']['carrier'])