#!/usr/bin/env python3 """Проверка canonical-ссылки страницы.""" from __future__ import annotations import argparse import json import re from pathlib import Path from urllib.parse import urljoin, urlsplit import requests from bs4 import BeautifulSoup def main() -> int: parser = argparse.ArgumentParser(description="Проверить canonical") parser.add_argument("url", nargs="?", default="https://example.com/") parser.add_argument("--timeout", type=float, default=15.0) parser.add_argument("--allow-cross-host", action="store_true") parser.add_argument("--json", type=Path, help="Сохранить отчёт") args = parser.parse_args() session = requests.Session() session.headers["User-Agent"] = "popovdev-scriptdock/1.0" try: response = session.get(args.url, timeout=args.timeout) response.raise_for_status() except requests.RequestException as error: print(f"Не удалось загрузить страницу: {error}") return 2 soup = BeautifulSoup(response.content, "html.parser") canonicals = [tag for tag in soup.find_all("link", href=True) if "canonical" in (tag.get("rel") or [])] robots_tag = soup.find("meta", attrs={"name": re.compile("^robots$", re.IGNORECASE)}) robots = (robots_tag.get("content") or "").lower() if robots_tag else "" problems = [] if not canonicals: problems.append("canonical не найден") if len(canonicals) > 1: problems.append(f"canonical встречается {len(canonicals)} раз") target = "" status = None if canonicals: href = (canonicals[0].get("href") or "").strip() if not href: problems.append("пустой canonical") else: href_parts = urlsplit(href) if not href_parts.scheme or not href_parts.netloc: problems.append("canonical не абсолютный") target = urljoin(response.url, href) parts = urlsplit(target) base_host = urlsplit(response.url).hostname if parts.hostname != base_host and not args.allow_cross_host: problems.append(f"canonical ведёт на другой хост: {parts.hostname}") try: check = session.head(target, timeout=args.timeout, allow_redirects=True) if check.status_code in {405, 501}: check = session.get(target, timeout=args.timeout, allow_redirects=True, stream=True) check.close() status = check.status_code if status >= 400: problems.append(f"canonical отвечает {status}") except requests.RequestException as error: problems.append(f"canonical недоступен: {error.__class__.__name__}") if "noindex" in robots: problems.append("canonical конфликтует с noindex") print(f"Страница: {response.url}") print(f"canonical: {target or 'нет'}") print(f"Код canonical: {status if status is not None else 'нет'}") if problems: print("Замечания:") for problem in problems: print(f" - {problem}") if args.json: report = {"url": args.url, "canonical": target, "status": status, "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 not problems else 1 if __name__ == "__main__": raise SystemExit(main())