Files
getDomain/domainCheck/app/detectors/baidu_detector.py
2026-04-16 13:33:06 +08:00

151 lines
4.7 KiB
Python

# -*- coding: UTF-8 -*-
'''
@Project :domainScanDemo
@File :baidu_detector.py
@IDE :PyCharm
@Author :梦伴
@Date :2026/4/8 23:58
@explain : 百度检测器
'''
import requests
from curl_cffi import requests as curl_requests
import re
from app.detectors.base import BaseDetector
class BaiduDetector(BaseDetector):
"""
百度检测器
"""
def __init__(self):
"""
初始化百度检测器
"""
super().__init__()
self.site_url = 'https://www.baidu.com/s'
self.history_url = 'https://www.baidu.com/s'
def check_domain(self, domain):
"""
检测域名
:param domain: 域名
:return: dict - 检测结果
"""
try:
# 检查百度site
site_result = self.check_site(domain)
# 检查百度历史
history_result = self.check_history(domain)
return {
'site': site_result,
'history': history_result
}
except Exception as e:
return self._handle_exception(e, domain)
def check_site(self, domain):
"""
检查百度site收录
:param domain: 域名
:return: dict - 检测结果
"""
try:
params = {
'wd': f'site:{domain}',
'rn': '50'
}
headers = {
'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'
}
# 使用curl_cffi模拟浏览器
response = curl_requests.get(self.site_url, params=params, headers=headers, impersonate='chrome', timeout=10)
if response.status_code == 200:
content = response.text
# 提取子域名
subdomains = self._extract_subdomains(content, domain)
# 检查是否有收录
has_收录 = '没有找到相关结果' not in content
return {
'has_收录': has_收录,
'subdomains': subdomains
}
else:
self._log_warning(f"百度site查询失败: {response.status_code}")
return {'has_收录': False, 'subdomains': []}
except Exception as e:
self._handle_exception(e, domain)
return {'has_收录': False, 'subdomains': []}
def check_history(self, domain):
"""
检查百度历史收录
:param domain: 域名
:return: dict - 检测结果
"""
try:
params = {
'wd': f'cache:{domain}',
'rn': '50'
}
headers = {
'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'
}
# 使用curl_cffi模拟浏览器
response = curl_requests.get(self.history_url, params=params, headers=headers, impersonate='chrome', timeout=10)
if response.status_code == 200:
content = response.text
# 检查是否有历史收录
has_history = '百度快照' in content
# 检查是否有灰色内容
has_gray = '风险提示' in content or '安全警告' in content
return {
'has_history': has_history,
'has_gray': has_gray
}
else:
self._log_warning(f"百度历史查询失败: {response.status_code}")
return {'has_history': False, 'has_gray': False}
except Exception as e:
self._handle_exception(e, domain)
return {'has_history': False, 'has_gray': False}
def _extract_subdomains(self, content, domain):
"""
提取子域名
:param content: 搜索结果内容
:param domain: 主域名
:return: list - 子域名列表
"""
try:
# 提取所有包含域名的链接
pattern = r'https?://([a-zA-Z0-9-]+)\.' + re.escape(domain)
matches = re.findall(pattern, content)
# 去重并过滤空值
subdomains = list(set(matches))
subdomains = [sub for sub in subdomains if sub]
return subdomains
except Exception as e:
self._handle_exception(e, domain)
return []