Files
niumalll/website/check_navigation.py

135 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""
导航栏一致性检查脚本
检查所有主要页面的导航栏是否包含一致的链接
"""
import os
import re
from bs4 import BeautifulSoup
# 要检查的页面列表
PAGES_TO_CHECK = [
'index.html',
'product.html',
'solutions.html',
'cases.html',
'about.html',
'news.html',
'contact.html',
'help.html',
'docs.html',
'partners.html',
'support.html'
]
# 期望的导航链接(按顺序)
EXPECTED_NAV_LINKS = [
('首页', 'index.html'),
('产品介绍', 'product.html'),
('解决方案', 'solutions.html'),
('客户案例', 'cases.html'),
('关于我们', 'about.html'),
('新闻动态', 'news.html'),
('帮助中心', 'help.html'),
('开发者文档', 'docs.html'),
('合作伙伴', 'partners.html'),
('在线客服', 'support.html'),
('联系我们', 'contact.html')
]
def check_navigation_consistency():
"""检查所有页面的导航栏一致性"""
website_dir = os.path.dirname(os.path.abspath(__file__))
results = {}
for page in PAGES_TO_CHECK:
page_path = os.path.join(website_dir, page)
if not os.path.exists(page_path):
print(f"⚠️ 页面不存在: {page}")
continue
try:
with open(page_path, 'r', encoding='utf-8') as f:
content = f.read()
soup = BeautifulSoup(content, 'html.parser')
# 查找导航栏
navbar = soup.find('nav', class_='navbar')
if not navbar:
print(f"⚠️ {page}: 未找到导航栏")
continue
# 查找导航链接
nav_links = []
nav_items = navbar.find_all('li', class_='nav-item')
for item in nav_items:
link = item.find('a', class_='nav-link')
if link:
nav_links.append({
'text': link.get_text(strip=True),
'href': link.get('href', ''),
'active': 'active' in link.get('class', [])
})
results[page] = {
'nav_links': nav_links,
'active_page': next((link['text'] for link in nav_links if link['active']), None)
}
print(f"{page}: 找到 {len(nav_links)} 个导航链接")
if results[page]['active_page']:
print(f" 当前活动页: {results[page]['active_page']}")
except Exception as e:
print(f"{page}: 解析错误 - {e}")
# 检查一致性
print("\n" + "="*50)
print("导航栏一致性检查结果:")
print("="*50)
all_consistent = True
for page, data in results.items():
print(f"\n📄 {page}:")
# 检查链接数量
if len(data['nav_links']) != len(EXPECTED_NAV_LINKS):
print(f" ❌ 链接数量不一致: 期望 {len(EXPECTED_NAV_LINKS)},实际 {len(data['nav_links'])}")
all_consistent = False
# 检查链接顺序和内容
for i, (expected_text, expected_href) in enumerate(EXPECTED_NAV_LINKS):
if i >= len(data['nav_links']):
print(f" ❌ 缺少链接: {expected_text} ({expected_href})")
all_consistent = False
continue
actual_link = data['nav_links'][i]
if actual_link['text'] != expected_text or actual_link['href'] != expected_href:
print(f" ❌ 链接不匹配: ")
print(f" 期望: {expected_text} -> {expected_href}")
print(f" 实际: {actual_link['text']} -> {actual_link['href']}")
all_consistent = False
# 检查活动页面
if data['active_page'] and page != 'index.html':
expected_active = next((text for text, href in EXPECTED_NAV_LINKS if href == page), None)
if data['active_page'] != expected_active:
print(f" ❌ 活动页面不正确: 期望 {expected_active},实际 {data['active_page']}")
all_consistent = False
if all_consistent:
print(f"\n🎉 所有页面导航栏一致!")
else:
print(f"\n⚠️ 存在不一致的导航栏,请检查上述问题")
return all_consistent
if __name__ == '__main__':
check_navigation_consistency()