1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| 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 = '' 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))
|