191 lines
8.2 KiB
Python
191 lines
8.2 KiB
Python
# -*- coding: UTF-8 -*-
|
||
'''
|
||
@Project :domainScanDemo
|
||
@File :c360.py
|
||
@IDE :PyCharm
|
||
@Author :梦伴
|
||
@Date :2026/4/2 15:57
|
||
@explain : 360搜索敏感词检测工具 - 检测域名是否包含敏感词
|
||
'''
|
||
|
||
import re # 正则表达式模块
|
||
import requests # HTTP请求库
|
||
from loguru import logger # 日志记录
|
||
from typing import List, Tuple, Optional, Dict # 类型提示
|
||
|
||
|
||
# 常量定义
|
||
SO_SEARCH_URL = 'https://www.so.com/s' # 360搜索URL
|
||
SO_REFERER_TEMPLATE = 'https://www.so.com/s?ie=utf-8&q=site%3A{domain}' # Referer模板
|
||
SEARCH_PATTERN = r'target="_blank">([^<]+)</a></h3>' # 搜索结果匹配模式
|
||
BLOCKED_CODE = 3 # 拦截状态码
|
||
|
||
|
||
def check_domain(
|
||
domain: str,
|
||
sensitive_words: Optional[List[str]] = None,
|
||
proxies: Optional[Dict] = None
|
||
) -> Tuple[bool, str]:
|
||
"""
|
||
检查域名是否包含敏感词
|
||
|
||
通过360搜索获取域名的搜索结果,然后检查搜索结果中是否包含敏感词
|
||
|
||
Args:
|
||
domain: 要检测的域名
|
||
sensitive_words: 敏感词列表,如果为None则初始化为空列表
|
||
proxies: 代理配置
|
||
|
||
Returns:
|
||
Tuple[bool, str]: (是否通过, 错误信息)
|
||
- True, '': 通过检测,不包含敏感词
|
||
- False, '敏感词:xxx': 包含敏感词,返回具体的敏感词
|
||
"""
|
||
# 初始化敏感词列表
|
||
if sensitive_words is None:
|
||
sensitive_words = []
|
||
|
||
# 构建请求头
|
||
headers = {
|
||
'Accept': '*/*', # 接受所有类型
|
||
'Accept-Language': 'zh-cn', # 中文语言
|
||
'Referer': SO_REFERER_TEMPLATE.format(domain=domain), # 来源页面
|
||
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36', # 用户代理
|
||
'Host': 'www.so.com', # 主机名
|
||
}
|
||
|
||
# 构建查询参数
|
||
params = {
|
||
'ie': 'utf-8', # 编码格式
|
||
'q': f'site:{domain}', # 搜索查询
|
||
}
|
||
|
||
# 添加重试机制
|
||
max_retries = 3
|
||
for retry in range(max_retries):
|
||
try:
|
||
# 发送GET请求获取搜索结果
|
||
response = requests.get(
|
||
SO_SEARCH_URL,
|
||
params=params,
|
||
headers=headers,
|
||
proxies=proxies,
|
||
timeout=15 # 增加超时时间到15秒
|
||
)
|
||
|
||
# 解析响应内容
|
||
response_html = response.content.decode('utf-8', errors='ignore')
|
||
|
||
# 使用正则表达式提取搜索结果
|
||
search_results = re.findall(SEARCH_PATTERN, response_html)
|
||
|
||
# 记录搜索结果数量
|
||
logger.info(f'360搜索结果数量: {len(search_results)}')
|
||
|
||
# 如果没有搜索结果,直接返回通过
|
||
if not search_results:
|
||
return True, ''
|
||
|
||
# 检查每个搜索结果是否包含敏感词
|
||
for result in search_results:
|
||
for sensitive_word in sensitive_words:
|
||
# 使用正则表达式检查是否包含敏感词
|
||
if re.search(sensitive_word, result, re.IGNORECASE):
|
||
logger.warning(f'检测到敏感词: {sensitive_word} 在结果: {result}')
|
||
return False, f'检测到敏感词: {sensitive_word} 在结果: {result}'
|
||
|
||
# 所有搜索结果都不包含敏感词,返回通过
|
||
return True, ''
|
||
|
||
except requests.exceptions.Timeout:
|
||
logger.warning(f'360搜索请求第{retry+1}次超时')
|
||
if retry < max_retries - 1:
|
||
import time
|
||
time.sleep(2) # 等待2秒后重试
|
||
else:
|
||
# 达到最大重试次数,尝试不使用代理
|
||
if proxies:
|
||
logger.info('360搜索请求超时,尝试不使用代理')
|
||
try:
|
||
response = requests.get(
|
||
SO_SEARCH_URL,
|
||
params=params,
|
||
headers=headers,
|
||
proxies=None,
|
||
timeout=15
|
||
)
|
||
response_html = response.content.decode('utf-8', errors='ignore')
|
||
search_results = re.findall(SEARCH_PATTERN, response_html)
|
||
logger.info(f'不使用代理的360搜索结果数量: {len(search_results)}')
|
||
if not search_results:
|
||
return True, ''
|
||
for result in search_results:
|
||
for sensitive_word in sensitive_words:
|
||
if re.search(sensitive_word, result, re.IGNORECASE):
|
||
logger.warning(f'检测到敏感词: {sensitive_word} 在结果: {result}')
|
||
return False, f'检测到敏感词: {sensitive_word} 在结果: {result}'
|
||
return True, ''
|
||
except Exception as e:
|
||
logger.error(f'不使用代理的360搜索请求也失败: {str(e)}')
|
||
return False, f'360搜索请求超时'
|
||
return False, '360搜索请求超时'
|
||
except requests.exceptions.RequestException as e:
|
||
error_str = str(e)
|
||
logger.warning(f'360搜索请求第{retry+1}次失败: {error_str}')
|
||
# 检查是否是"Too many open files"错误
|
||
if 'Too many open files' in error_str:
|
||
if retry < max_retries - 1:
|
||
import time
|
||
time.sleep(2) # 等待2秒后重试
|
||
else:
|
||
# 达到最大重试次数,返回failure标记,不拉黑域名
|
||
logger.error(f'360搜索请求失败,已达到最大重试次数: {error_str}')
|
||
return True, 'failure'
|
||
else:
|
||
if retry < max_retries - 1:
|
||
import time
|
||
time.sleep(2) # 等待2秒后重试
|
||
else:
|
||
# 达到最大重试次数,尝试不使用代理
|
||
if proxies:
|
||
logger.info('360搜索请求失败,尝试不使用代理')
|
||
try:
|
||
response = requests.get(
|
||
SO_SEARCH_URL,
|
||
params=params,
|
||
headers=headers,
|
||
proxies=None,
|
||
timeout=15
|
||
)
|
||
response_html = response.content.decode('utf-8', errors='ignore')
|
||
search_results = re.findall(SEARCH_PATTERN, response_html)
|
||
logger.info(f'不使用代理的360搜索结果数量: {len(search_results)}')
|
||
if not search_results:
|
||
return True, ''
|
||
for result in search_results:
|
||
for sensitive_word in sensitive_words:
|
||
if re.search(sensitive_word, result, re.IGNORECASE):
|
||
logger.warning(f'检测到敏感词: {sensitive_word} 在结果: {result}')
|
||
return False, f'检测到敏感词: {sensitive_word} 在结果: {result}'
|
||
return True, ''
|
||
except Exception as e2:
|
||
logger.error(f'不使用代理的360搜索请求也失败: {str(e2)}')
|
||
return False, f'360搜索请求失败: {str(e)}'
|
||
return False, f'360搜索请求失败: {str(e)}'
|
||
except Exception as e:
|
||
logger.warning(f'360搜索第{retry+1}次未知错误: {str(e)}')
|
||
if retry < max_retries - 1:
|
||
import time
|
||
time.sleep(2) # 等待2秒后重试
|
||
else:
|
||
return False, f'未知错误: {str(e)}'
|
||
|
||
|
||
# if __name__ == '__main__':
|
||
# # 测试用例
|
||
# test_domain = 'niuniushouka.com'
|
||
# test_sensitive_words = ['赌博', '色情', '暴力','收卡']
|
||
#
|
||
# logger.info(f'开始检测域名: {test_domain}')
|
||
# result = check_domain(test_domain, test_sensitive_words)
|
||
# logger.info(f'检测结果: {result}') |