调用淘宝接口查询IP所在地

来源:互联网 发布:youtube无法连接网络 编辑:程序博客网 时间:2024/04/29 05:40
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
IP = raw_input("请输入:");
def checkip(ip):
 
  URL = 'http://ip.taobao.com/service/getIpInfo.php'
  try:
    r = requests.get(URL, params=ip, timeout=3)
  except requests.RequestException as e:
    print(e)
  else:
    json_data = r.json()
    if json_data[u'code'] == 0:
      print ('所在国家: ' + json_data[u'data'][u'country'].encode('utf-8'))
      print ('所在地区: ' + json_data[u'data'][u'area'].encode('utf-8'))
      print ('所在省份: ' + json_data[u'data'][u'region'].encode('utf-8'))
      print ('所在城市: ' + json_data[u'data'][u'city'].encode('utf-8'))
      print ('所属运营商:' + json_data[u'data'][u'isp'].encode('utf-8'))
    else:
      print ('查询失败,请稍后再试!')                                                                                                                                  
 
ip={'ip': IP} 
checkip(ip)
0 0