# -*- coding: utf-8 -*- import requests import json def get_physical_location(ip): url = ‘http://ip.taobao.com/service/getIpInfo.php?ip=‘ + ip ip_data = requests.get(url).text ip_dict = json.loads(ip_data) if ip_dict.get(‘code‘): print(‘你输入的ip地址格式有误,请仔细检查。‘) else: country = ip_dict.get(‘data‘).get(‘country‘) region = ip_dict.get(‘data‘).get(‘region‘) city = ip_dict.get(‘data‘).get(‘city‘) return country + region + city if __name__ == ‘__main__‘: print get_physical_location(‘8.8.8.8‘)
时间: 2024-10-10 07:36:21