Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

feat(fetch-page): add JSON output for raw and rendered fetches

Roni Veghe7fdd9b042ce
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 047c507..85c28ea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Added
+
+- `fetch_page.py --json` exposes full response metadata and content for raw and
+ rendered fetches, including structured fetch errors (#282).
+
## [2.2.6] - 2026-09-10
### Security
diff --git a/docs/COMMANDS.md b/docs/COMMANDS.md
index a86888d..140f6f5 100644
--- a/docs/COMMANDS.md
+++ b/docs/COMMANDS.md
@@ -4,6 +4,20 @@
All Claude SEO commands start with `/seo` followed by a subcommand.
+## Page-fetching script
+
+Use `./bin/claude-seo run fetch_page.py https://example.com --json` for structured
+retrieval. JSON includes `url`, `status_code`, `content`, `headers`,
+`redirect_chain`, and `error`. Raw fetches also include `redirect_details`;
+`--render auto` or `--render always` includes the renderer's additional fields.
+Content is returned in full. Fetch errors still emit JSON and exit 1; successful
+fetches exit 0. HTTP status codes are reported without changing this behavior.
+
+Combine `--json --output page.html` to save successful HTML while keeping JSON
+on stdout. `output_written` indicates whether HTML was saved. On fetch errors,
+the output file is left untouched. Without `--json`, existing text output is
+unchanged.
+
## Command List
### `/seo setup`
diff --git a/scripts/fetch_page.py b/scripts/fetch_page.py
index fb8826f..7e81ffb 100755
--- a/scripts/fetch_page.py
+++ b/scripts/fetch_page.py
@@ -9,6 +9,7 @@ SPA-aware fetching.
Usage:
python fetch_page.py https://example.com
python fetch_page.py https://example.com --output page.html
+ python fetch_page.py https://example.com --json
python fetch_page.py https://example.com --render auto # SPA-aware
python fetch_page.py https://example.com --render always # force render
"""
@@ -16,6 +17,7 @@ Usage:
from __future__ import annotations
import argparse
+import json
import os
import re
import sys
@@ -195,10 +197,22 @@ def fetch_page(
return result
+def _emit_json(result: dict, output: Optional[str]) -> None:
+ """Emit a fetch result, optionally saving successful HTML separately."""
+ output_written = False
+ if output and not result["error"]:
+ with open(output, "w", encoding="utf-8") as f:
+ f.write(result["content"] or "")
+ output_written = True
+ print(json.dumps({**result, "output_written": output_written}, indent=2))
+ sys.exit(1 if result["error"] else 0)
+
+
def main():
parser = argparse.ArgumentParser(description="Fetch a web page for SEO analysis")
parser.add_argument("url", help="URL to fetch")
parser.add_argument("--output", "-o", help="Output file path")
+ parser.add_argument("--json", action="store_true", help="Emit full fetch result as JSON")
parser.add_argument("--timeout", "-t", type=int, default=30, help="Timeout in seconds")
parser.add_argument("--no-redirects", action="store_true", help="Don't follow redirects")
parser.add_argument("--user-agent", help="Custom User-Agent string")
@@ -237,6 +251,8 @@ def main():
timeout_ms=args.timeout * 1000,
user_agent=ua,
)
+ if args.json:
+ _emit_json(rendered, args.output)
if rendered["error"]:
print(f"Error: {rendered['error']}", file=sys.stderr)
sys.exit(1)
@@ -261,6 +277,9 @@ def main():
user_agent=ua,
)
+ if args.json:
+ _emit_json(result, args.output)
+
if result["error"]:
print(f"Error: {result['error']}", file=sys.stderr)
sys.exit(1)
diff --git a/tests/test_fetch_page_cli.py b/tests/test_fetch_page_cli.py
new file mode 100644
index 0000000..9df38de
--- /dev/null
+++ b/tests/test_fetch_page_cli.py
@@ -0,0 +1,84 @@
+"""CLI output contracts for raw and rendered page fetching."""
+
+import json
+import sys
+from pathlib import Path
+
+import pytest
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
+import fetch_page # noqa: E402
+import render_page # noqa: E402
+
+
[email protected](params=["never", "auto", "always"])
+def fetch_result(request, monkeypatch):
+ result = {
+ "url": "https://example.com/final",
+ "status_code": 200,
+ "content": '<p>Café "hello"\n世界</p>' + "x" * 12000,
+ "headers": {"Content-Type": "text/html; charset=utf-8"},
+ "redirect_chain": ["https://example.com"],
+ "redirect_details": [{"url": "https://example.com", "status_code": 301}],
+ "error": None,
+ }
+ if request.param == "never":
+ monkeypatch.setattr(fetch_page, "fetch_page", lambda *a, **kw: result)
+ else:
+ result.update(mode_used="rendered", is_spa=True, render_ms=12.5)
+ monkeypatch.setattr(render_page, "render_page", lambda *a, **kw: result)
+ monkeypatch.setattr(sys, "argv", ["fetch_page.py", "https://example.com", "--render", request.param])
+ return result
+
+
[email protected]("save_html", [False, True])
+def test_json_preserves_full_result(fetch_result, save_html, tmp_path, capsys):
+ sys.argv += ["--json"]
+ output = tmp_path / "page.html"
+ if save_html:
+ sys.argv += ["--output", str(output)]
+ with pytest.raises(SystemExit) as exc:
+ fetch_page.main()
+ assert exc.value.code == 0
+ captured = capsys.readouterr()
+ assert json.loads(captured.out) == {**fetch_result, "output_written": save_html}
+ assert captured.err == ""
+ assert output.exists() == save_html
+ if save_html:
+ assert output.read_text(encoding="utf-8") == fetch_result["content"]
+
+
+def test_json_fetch_error_preserves_existing_file(fetch_result, tmp_path, capsys):
+ fetch_result.update(error="Request timed out", content=None, status_code=None)
+ output = tmp_path / "page.html"
+ output.write_text("existing", encoding="utf-8")
+ sys.argv += ["--json", "--output", str(output)]
+ with pytest.raises(SystemExit) as exc:
+ fetch_page.main()
+ assert exc.value.code == 1
+ captured = capsys.readouterr()
+ assert json.loads(captured.out) == {**fetch_result, "output_written": False}
+ assert captured.err == ""
+ assert output.read_text(encoding="utf-8") == "existing"
+
+
+def test_json_http_error_status_is_still_a_completed_fetch(fetch_result, capsys):
+ fetch_result["status_code"] = 404
+ sys.argv += ["--json"]
+ with pytest.raises(SystemExit) as exc:
+ fetch_page.main()
+ assert exc.value.code == 0
+ assert json.loads(capsys.readouterr().out)["status_code"] == 404
+
+
[email protected]("save_html", [False, True])
+def test_text_output_is_unchanged(fetch_result, save_html, tmp_path, capsys):
+ output = tmp_path / "page.html"
+ if save_html:
+ sys.argv += ["--output", str(output)]
+ fetch_page.main()
+ captured = capsys.readouterr()
+ expected = f"Saved to {output}" if save_html else fetch_result["content"]
+ assert captured.out == expected + "\n"
+ assert "URL: https://example.com/final" in captured.err
+ assert "Status: 200" in captured.err