Python实现查询域名的IP地址/归属地/备案信息

import json
import socket

import requests
from lxml import etree
from tld import get_fld

url = 'https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={}&resource_id=5809&format=json'
record_url = 'https://icp.chinaz.com/{}'
proxies = {
    
    "http": None, "https": None}


class GetHost:
    # 域名
    domain = ''
    # IP地址
    ip = ''
    # IP归属地
    location = ''
    # 主办单位名称
    org_name = ''
    # 主办单位性质
    sponsor_nature = ''
    # 网站名称
    website_name = ''

    def __init__(self, domain, ip):
        self.domain = domain
        self.ip = ip
        self.get_host_info()

    def get_host_info(self):
        if self.ip == '':
            self.ip = socket.gethostbyname(self.domain)
        response = requests.get(url.format(self.ip), proxies=proxies)
        json_obj = json.loads(response.text)
        info = json_obj['data']
        self.location = info[0]['location']
        if self.domain != '':
            response = requests.get(record_url.format(get_fld(self.domain, fix_protocol=True)),
                                    proxies=proxies)
            html = etree.HTML(response.content)
            record_info = html.xpath('//li[@class=\'bg-gray clearfix\']')
            self.org_name = record_info[0].xpath('./p/a/text()')[0]
            record_info_1 = html.xpath('//li[@class=\'clearfix\']')
            self.sponsor_nature = record_info_1[0].xpath('./p/strong/text()')[0]
            self.website_name = record_info_1[1].xpath('./p/text()')[0]


if __name__ == '__main__':
    host = GetHost('www.qq.com', '')
    print('域名:{}'.format(host.domain))
    print('IP:{}'.format(host.ip))
    print('归属地:{}'.format(host.location))
    print('主办单位名称:{}'.format(host.org_name))
    print('主办单位性质:{}'.format(host.sponsor_nature))
    print('网站名称:{}'.format(host.website_name))

输出结果

域名:www.qq.com
IP:101.91.22.57
归属地:上海市 电信
主办单位名称:深圳市腾讯计算机系统有限公司
主办单位性质:企业
网站名称:腾讯网

猜你喜欢

转载自blog.csdn.net/y1534414425/article/details/128496661