为了方便平时对IP以及涉及的域名进行相关信息查询,用python写了一个简单的脚本,方便平时使用。主要利用到ip138网站进行IP反查和域名IP解析记录查询以及爱站网相关域名权重查询。欢迎师傅们进行相关优化,脚本代码如下:
import sys
import time
import requests
from lxml import etree
# ip查询域名或域名解析查询
def query(ip_or_domain):
url = "https://site.ip138.com/{}/".format(ip_or_domain)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0",
"Accept": "* / *"
}
response = requests.get(url, headers=headers)
# print(response.text)
if module == 1:
ipResult(response)
if module == 2:
domainResult(response)
# SEO权重查询
def query_seo(domain):
url = "https://www.aizhan.com/cha/{}/".format(domain)
headers = {
"Cookie": "Hm_lvt_b37205f3f69d03924c5447d020c09192=1672124272,1672735140,1673514737; _csrf=a0f57c688194fe82afecf61cbea74d0da248568a19474440a60b66646046e3b0a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22sNoA1RLB3SXLCr_--Dgjfu6J-8PtyCfy%22%3B%7D; allSites=www.baidu.com%7Cpassport.vivo.com.cn%7Cexp.scjgj.sh.gov.cn%7Ccoscoairprod.5156.com.cn%7Cjiekou.sz517.com%7Cgo.huanqiu.com%7Cmiaozhan-admin.sto.cn%7Cshenyunduo.sto.cn%7Cassets.plesk.com%7Capp.mokahr.com; Hm_lpvt_b37205f3f69d03924c5447d020c09192=1673516067",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
}
response = requests.get(url, headers=headers)
# print(response.text)
SEOResult(response)
# 匹配IP查询结果
def ipResult(response):
# 利用etree.HTML将字符串解析为HTML文档
html = etree.HTML(response.text)
# print(type(html))
date_result = html.xpath('//ul[@id="list"]/li/span//text()')
domain_result = html.xpath('//ul[@id="list"]/li/a//text()')
error_result = html.xpath('//p[@class="error"]//text()')
# print(type(date))
# print(len(date))
# print(len(error))
if len(error_result) == 1:
print("禁止查询该IP!!!")
else:
formatIPResult(date_result, domain_result)
# 匹配域名查询结果
def domainResult(response):
# 利用etree.HTML将字符串解析为HTML文档
html = etree.HTML(response.text)
# print(html)
domain_result = html.xpath('//div[@id="J_ip_history"]//text()')
error_result = html.xpath('//p[@class="error"]//text()')
# print(type(domain_result))
# print(type(domain_result))
# print(len(domain))
if len(error_result) == 1:
print("禁止查询该域名!!!")
else:
formatDomainResult(domain_result)
# 匹配SEO查询结果
def SEOResult(response):
html = etree.HTML(response.text)
# print(html)
engine_result = html.xpath('//table[@class="table"]//li/text()')
weight_result = html.xpath('//table[@class="table"]//img/@src')
# print(engine_result)
# print(weight_result)
formatSEOResult(engine_result, weight_result)
# 美化打印IP查询结果
def formatIPResult(date_result, domain_result):
print("==================" + input_ip + date_result[0] + "================")
for i in date_result:
if date_result.index(i) != 0:
print(date_result[date_result.index(i)] + ":", end="")
print(domain_result[date_result.index(i) - 1])
# 美化打印域名查询结果
def formatDomainResult(domain_result):
list_ = []
print("==================" + input_domain + "历史IP解析记录如下:" + "=================")
# 去除解析获取列表中换行符并将其数据存储至新的list_列表
for i in domain_result:
if i.strip():
list_.append(i.strip())
# print(list_)
for i in list_:
if list_.index(i) % 2 == 0:
# print(list_.index(i))
print(i + ":", end="")
else:
print(i)
# 美化打印SEO匹配结果
def formatSEOResult(engine_result, weight_result):
engine_name = []
weight_value = []
print("==================" + input_domain + " SEO综合查询如下:" + "=================")
for i in engine_result:
if 5 < engine_result.index(i) < 12:
# print(i)
engine_name.append(i)
del weight_result[0] # 删除列表第一个值
# print(weight_result)
for j in weight_result:
# print(j)
# print(j.split('/')[-1])
# print(j.split('/')[-1].split('.')[0])
value = j.split('/')[-1].split('.')[0] # 通过获取的图片url,进行优化提取权重值
# print(value)
weight_value.append(value)
# print(engine_name)
# print(weight_value)
for k in engine_name:
print(engine_name[engine_name.index(k)], end="")
print(weight_value[engine_name.index(k)])
if __name__ == "__main__":
# 获取当前时间
# localtime = time.strftime('%Y-%m-%D %H-%M-%S', time.localtime())
print("==================IP/域名/SEO查询========Author:magic_rookie==")
flag = 1
while flag:
try:
module = int(input("请输入要查询的功能(1.IP查询 2.域名查询 3.SEO查询 0.退出程序):"))
# print(type(int(module)))
# i = 3
# print(type(i))
if module < 0 or module > 3:
sys.exit()
if module == 1:
# input_ip = "123.6.81.231"
input_ip = input("请输入要查询的IP:")
query(input_ip)
if module == 2:
# input_domain = "hydcd.com"
input_domain = input("请输入要查询的域名:")
query(input_domain)
if module == 3:
# input_domain = "www.baidu.com"
input_domain = input("请输入要查询的域名:")
query_seo(input_domain)
if module == 0:
flag = 0
print("程序退出!!!")
except:
print("输入有误或程序运行异常或网络异常,请重试!!!")
运行结果: