您的位置:首页 > 其它

爬虫实例3——获取指定IP地址的物理位置

2015-06-23 10:34 330 查看
# -*- 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')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  爬虫 IP 物理位置