140 lines
5.2 KiB
Python
140 lines
5.2 KiB
Python
# -*- coding: UTF-8 -*-
|
||
'''
|
||
@Project :domainScanDemo
|
||
@File :aizhan.py
|
||
@IDE :PyCharm
|
||
@Author :梦伴
|
||
@Date :2026/3/29 20:59
|
||
@explain : 爱站网域名查询工具 - 获取域名的网站标题信息
|
||
'''
|
||
|
||
import re # 正则表达式模块,用于从HTML中提取网站标题
|
||
import os
|
||
|
||
import requests # HTTP请求库,用于发送网络请求
|
||
from loguru import logger # 日志记录模块,用于记录程序运行日志
|
||
from requests.adapters import HTTPAdapter
|
||
|
||
from typing import List, Optional # 类型提示
|
||
|
||
|
||
_SESSION = requests.Session()
|
||
_SESSION.mount("http://", HTTPAdapter(pool_connections=256, pool_maxsize=512, max_retries=0))
|
||
_SESSION.mount("https://", HTTPAdapter(pool_connections=256, pool_maxsize=512, max_retries=0))
|
||
AIZHAN_TIMEOUT_PROXY = max(
|
||
0.8,
|
||
float(os.getenv("DOMAINCHECK_AIZHAN_TIMEOUT_PROXY", "1.6") or 1.6),
|
||
)
|
||
AIZHAN_TIMEOUT_DIRECT = max(
|
||
1.0,
|
||
float(os.getenv("DOMAINCHECK_AIZHAN_TIMEOUT_DIRECT", "2.2") or 2.2),
|
||
)
|
||
|
||
|
||
def _resolve_aizhan_timeout(proxies: dict = None, budget_seconds: Optional[float] = None) -> float:
|
||
timeout = float(AIZHAN_TIMEOUT_PROXY if proxies else AIZHAN_TIMEOUT_DIRECT)
|
||
if budget_seconds not in (None, "", 0, "0"):
|
||
timeout = min(timeout, max(0.6, float(budget_seconds or 0.0)))
|
||
return max(0.6, timeout)
|
||
|
||
|
||
def check_aizhan(
|
||
domain: str,
|
||
sensitive_words: Optional[List[str]] = None,
|
||
proxies: dict = None,
|
||
budget_seconds: Optional[float] = 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', # 目标主机地址
|
||
}
|
||
|
||
try:
|
||
# 发送GET请求获取页面内容
|
||
# url: 请求URL
|
||
# headers: HTTP请求头
|
||
# timeout=10: 设置超时时间10秒
|
||
# proxies: 代理配置(如需使用代理)
|
||
response = _SESSION.get(
|
||
url,
|
||
headers=headers,
|
||
timeout=_resolve_aizhan_timeout(proxies, budget_seconds=budget_seconds),
|
||
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.debug(f"爱站网检测失败: {domain}, 状态码: {response.status_code}")
|
||
return True, ''
|
||
|
||
except Exception as e: # 捕获所有异常(网络错误、超时等)
|
||
logger.debug(f"爱站网检测失败: {domain}, 错误: {e}")
|
||
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 '(无结果)'}")
|