python-通过域名获取该域名所属国家及地区

# encoding:utf-8
import requests
import socket


def getIP(domain):
    """通过域名获取IP"""
    myaddr = socket.getaddrinfo(domain, None)
    return myaddr[0][4][0]


def get_ip_info(ip):
    """通过IP获取所在低信息"""
    r = requests.get('http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip)
    if r.json()['code'] == 0:
        i = r.json()['data']
        country = i['country']  # 国家
        area = i['area']  # 区域
        region = i['region']  # 地区
        city = i['city']  # 城市
        isp = i['isp']  # 运营商

        # print(u'国家: %s\n区域: %s\n省份: %s\n城市: %s\n运营商: %s\n' % (country, area, region, city, isp))
        return country
    else:
        print "ERROR! ip: %s" % ip
        return None


if __name__ == '__main__':
    IP = getIP("www.sqsglj.com")   # 注意: 这里输入的域名不包含http及https前缀
    country = get_ip_info(IP)
    if not country:
        country = "无法识别该网站所属地"
    print("该网站的所属国家为: %s" % country.encode('utf-8'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值