#!/usr/bin/env python3 """Проверка базовых meta-тегов.""" from __future__ import annotations import argparse import json import re from pathlib import Path import requests from bs4 import BeautifulSoup def content_of(soup: BeautifulSoup, name: str) -> str: tag = soup.find("meta", attrs={"name": re.compile(f"^{re.escape(name)}$", re.IGNORECASE)}) return (tag.get("content") or "").strip() if tag else "" def main() -> int: parser = argparse.ArgumentParser(description="Проверить meta-теги") parser.add_argument("url", nargs="?", default="https://example.com/") parser.add_argument("--timeout", type=float, default=15.0) parser.add_argument("--no-fail", action="store_true", help="Всегда возвращать 0") parser.add_argument("--json", type=Path, help="Сохранить отчёт") args = parser.parse_args() try: response = requests.get(args.url, timeout=args.timeout, headers={"User-Agent": "popovdev-scriptdock/1.0"}) response.raise_for_status() except requests.RequestException as error: print(f"Не удалось загрузить страницу: {error}") return 2 soup = BeautifulSoup(response.content, "html.parser") title = soup.title.get_text(" ", strip=True) if soup.title else "" description = content_of(soup, "description") robots = content_of(soup, "robots") viewport = content_of(soup, "viewport") charset_tag = soup.find("meta", attrs={"charset": True}) charset = charset_tag.get("charset", "") if charset_tag else "" http_equiv = soup.find("meta", attrs={"http-equiv": re.compile("^content-type$", re.IGNORECASE)}) if not charset and http_equiv: charset = http_equiv.get("content", "") html_tag = soup.find("html") lang = (html_tag.get("lang") or "").strip() if html_tag else "" descriptions = soup.find_all("meta", attrs={"name": re.compile("^description$", re.IGNORECASE)}) problems = [] if not title: problems.append("нет title") elif not 15 <= len(title) <= 60: problems.append(f"длина title: {len(title)}") if not description: problems.append("нет meta description") elif not 50 <= len(description) <= 160: problems.append(f"длина description: {len(description)}") if not charset: problems.append("не указан charset") if not viewport: problems.append("нет meta viewport") if not lang: problems.append("у html нет lang") if len(descriptions) > 1: problems.append(f"description встречается {len(descriptions)} раз") if "noindex" in robots.lower(): problems.append("страница закрыта от индексации") print(f"title ({len(title)}): {title}") print(f"description ({len(description)}): {description[:160]}") print(f"charset: {charset or 'нет'}") print(f"viewport: {viewport or 'нет'}") print(f"lang: {lang or 'нет'}") print(f"robots: {robots or 'нет'}") if problems: print("Замечания:") for problem in problems: print(f" - {problem}") if args.json: report = { "url": args.url, "title": title, "description": description, "charset": charset, "viewport": viewport, "lang": lang, "robots": robots, "problems": problems, } args.json.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(f"Отчёт: {args.json}") return 0 if args.no_fail or not problems else 1 if __name__ == "__main__": raise SystemExit(main())