first commit
This commit is contained in:
155
detect/aizhan.py
Normal file
155
detect/aizhan.py
Normal file
@@ -0,0 +1,155 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
'''
|
||||
@Project :domainScanDemo
|
||||
@File :aizhan.py
|
||||
@IDE :PyCharm
|
||||
@Author :梦伴
|
||||
@Date :2026/3/29 20:59
|
||||
@explain : 爱站网域名查询工具 - 获取域名的网站标题信息
|
||||
'''
|
||||
|
||||
import re # 正则表达式模块,用于从HTML中提取网站标题
|
||||
|
||||
import requests # HTTP请求库,用于发送网络请求
|
||||
from loguru import logger # 日志记录模块,用于记录程序运行日志
|
||||
|
||||
from typing import List, Optional # 类型提示
|
||||
|
||||
|
||||
def check_aizhan(domain: str, sensitive_words: Optional[List[str]] = None, proxies: dict = None):
|
||||
'''
|
||||
查询域名的网站标题信息是否存在敏感词
|
||||
|
||||
通过爱站网(aizhan.com)查询域名的网站标题
|
||||
|
||||
:param domain: 待查询的域名(如:www.baidu.com,不带协议头)
|
||||
:param proxies: 代理配置(如:{'http': 'http://127.0.0.1:7890', 'https': 'http://127.0.0.1:7890'}
|
||||
:return: str - 网站标题字符串,如果查询失败则返回空字符串
|
||||
'''
|
||||
# 初始化敏感词列表
|
||||
if sensitive_words is None:
|
||||
sensitive_words = []
|
||||
|
||||
# 构建爱站网查询URL
|
||||
# 格式:https://www.aizhan.com/cha/{域名}/
|
||||
url = f'https://www.aizhan.com/cha/{domain}/'
|
||||
|
||||
# 构建HTTP请求头(模拟浏览器访问)
|
||||
headers = {
|
||||
'Accept': '*/*', # 接受所有类型的内容
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0',
|
||||
# 用户代理(浏览器标识)
|
||||
'Referer': url, # 来源页面(防盗链)
|
||||
'host': 'www.aizhan.com', # 目标主机地址
|
||||
}
|
||||
|
||||
# 添加重试机制
|
||||
max_retries = 3
|
||||
for retry in range(max_retries):
|
||||
try:
|
||||
# 发送GET请求获取页面内容
|
||||
# url: 请求URL
|
||||
# headers: HTTP请求头
|
||||
# timeout=10: 设置超时时间10秒
|
||||
# proxies: 代理配置(如需使用代理)
|
||||
response = requests.get(url, headers=headers, timeout=10, proxies=proxies)
|
||||
|
||||
# 判断请求是否成功(HTTP 200表示成功)
|
||||
if response.status_code == 200:
|
||||
# 使用正则表达式提取网站标题
|
||||
# 正则解释:匹配 id="webpage_title"> 开头,</div> 结尾,中间的内容
|
||||
# 匹配模式:id="webpage_title">标题内容</div>
|
||||
# ([^<]+) 表示匹配一个或多个非<字符,作为捕获组
|
||||
match = re.search(r'id="webpage_title">([^<]+)</div>', response.text)
|
||||
|
||||
# 如果找到匹配则返回标题文本,否则返回空字符串
|
||||
title = match.group(1) if match else ''
|
||||
if title:
|
||||
for sensitive_word in sensitive_words:
|
||||
# 使用正则表达式检查是否包含敏感词
|
||||
if re.search(sensitive_word, title, re.IGNORECASE):
|
||||
logger.warning(f'检测到敏感词: {sensitive_word} 在结果: {title}')
|
||||
return False, f'检测到敏感词: {sensitive_word} 在结果: {title}'
|
||||
return True, title
|
||||
|
||||
# 请求失败,继续重试
|
||||
logger.warning(f"爱站网检测第{retry+1}次失败: {domain}, 状态码: {response.status_code}")
|
||||
if retry < max_retries - 1:
|
||||
import time
|
||||
time.sleep(2) # 等待2秒后重试
|
||||
else:
|
||||
# 达到最大重试次数,尝试不使用代理
|
||||
if proxies:
|
||||
logger.info('爱站网检测失败,尝试不使用代理')
|
||||
try:
|
||||
response = requests.get(url, headers=headers, timeout=10, proxies=None)
|
||||
if response.status_code == 200:
|
||||
match = re.search(r'id="webpage_title">([^<]+)</div>', response.text)
|
||||
title = match.group(1) if match else ''
|
||||
if title:
|
||||
for sensitive_word in sensitive_words:
|
||||
if re.search(sensitive_word, title, re.IGNORECASE):
|
||||
logger.warning(f'检测到敏感词: {sensitive_word} 在结果: {title}')
|
||||
return False, f'检测到敏感词: {sensitive_word} 在结果: {title}'
|
||||
return True, title
|
||||
return True, ''
|
||||
except Exception as e2:
|
||||
logger.error(f"不使用代理的爱站网检测也失败: {domain}, 错误: {e2}")
|
||||
return False, str(e2)
|
||||
return True, ''
|
||||
|
||||
except Exception as e: # 捕获所有异常(网络错误、超时等)
|
||||
logger.warning(f"爱站网检测第{retry+1}次失败: {domain}, 错误: {e}")
|
||||
if retry < max_retries - 1:
|
||||
import time
|
||||
time.sleep(2) # 等待2秒后重试
|
||||
else:
|
||||
# 达到最大重试次数,尝试不使用代理
|
||||
if proxies:
|
||||
logger.info('爱站网检测失败,尝试不使用代理')
|
||||
try:
|
||||
response = requests.get(url, headers=headers, timeout=10, proxies=None)
|
||||
if response.status_code == 200:
|
||||
match = re.search(r'id="webpage_title">([^<]+)</div>', response.text)
|
||||
title = match.group(1) if match else ''
|
||||
if title:
|
||||
for sensitive_word in sensitive_words:
|
||||
if re.search(sensitive_word, title, re.IGNORECASE):
|
||||
logger.warning(f'检测到敏感词: {sensitive_word} 在结果: {title}')
|
||||
return False, f'检测到敏感词: {sensitive_word} 在结果: {title}'
|
||||
return True, title
|
||||
return True, ''
|
||||
except Exception as e2:
|
||||
logger.error(f"不使用代理的爱站网检测也失败: {domain}, 错误: {e2}")
|
||||
return False, str(e2)
|
||||
return False, str(e)
|
||||
|
||||
# # ========================= 【测试代码】 =========================
|
||||
# if __name__ == '__main__':
|
||||
# # 测试用例1:查询常见域名
|
||||
# logger.info("=== 测试爱站网查询 ===")
|
||||
# test_domain_1 = 'baidu.com'
|
||||
# logger.info(f"查询域名: {test_domain_1}")
|
||||
#
|
||||
# result_1 = check_aizhan(test_domain_1)
|
||||
# logger.info(f"网站标题: {result_1}")
|
||||
# logger.info("")
|
||||
#
|
||||
# # 测试用例2:查询另一个域名
|
||||
# logger.info("=== 测试查询QQ域名 ===")
|
||||
# test_domain_2 = 'qq.com'
|
||||
# logger.info(f"查询域名: {test_domain_2}")
|
||||
#
|
||||
# result_2 = check_aizhan(test_domain_2)
|
||||
# logger.info(f"网站标题: {result_2}")
|
||||
# logger.info("")
|
||||
#
|
||||
#
|
||||
#
|
||||
# # 测试用例4:查询可能不存在的域名
|
||||
# logger.info("=== 测试不存在域名 ===")
|
||||
# test_domain_4 = 'nonexistentdomain123456.com'
|
||||
# logger.info(f"查询域名: {test_domain_4}")
|
||||
#
|
||||
# result_4 = check_aizhan(test_domain_4)
|
||||
# logger.info(f"网站标题: {result_4 if result_4 else '(无结果)'}")
|
||||
Reference in New Issue
Block a user