"""对真实站点做连通性与结构验证。 注意:biquge.tw 在当前开发网络不可达(DNS 解析到被屏蔽的 IP), 请在有网络权限的环境下运行本脚本。 用法: python scripts/verify_site.py # 验证首页:分类/搜索/最近更新 python scripts/verify_site.py --book 47_47879 # 验证一本书的目录 python scripts/verify_site.py --chapter 47_47879 3155529 # 验证一章正文 """ from __future__ import annotations import argparse import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from app.crawler.client import FetchError from app.crawler.scraper import BiqugeScraper, NotFoundError, ScraperError from app.crawler.site import SiteConfig def main() -> int: parser = argparse.ArgumentParser(description="验证 biquge.tw 爬虫") parser.add_argument("--config", default="sites/biquge_tw.json", help="站点配置文件路径") parser.add_argument("--book", help="要验证的书籍 ID,如 47_47879") parser.add_argument("--chapter", nargs=2, metavar=("BOOK_ID", "CHAPTER_ID"), help="验证单章") parser.add_argument("--max-categories", type=int, default=30) args = parser.parse_args() config = SiteConfig.from_json(args.config) scraper = BiqugeScraper(config) try: print(f"== 站点: {config.name} ({config.base_url}) ==") home = scraper.fetch_home(refresh=True) print(f"\n[首页] 自动发现分类 {len(home['categories'])} 个:") for item in home["categories"][: args.max_categories]: print(f" - {item['name']} id={item['id']} url={item['url']}") print(f"\n[首页] 最近更新 {len(home['recent'])} 本:") for item in home["recent"][:10]: print(f" - {item['title']} book_id={item['book_id']}") search = scraper._search_info() print(f"\n[搜索] 自动发现的接口: method={search['method']} " f"url={search['url_template']} param={search['param']}") if args.book: book = scraper.fetch_book(args.book) print(f"\n[书籍] {book['title']} 作者={book['author']} 状态={book['status']}") print(f" 章节数: {book['chapter_count']}") for chapter in book["chapters"][:5]: print(f" - {chapter['chapter_id']} {chapter['title']}") print(" ...") for chapter in book["chapters"][-3:]: print(f" - {chapter['chapter_id']} {chapter['title']}") if args.chapter: book_id, chapter_id = args.chapter chapter = scraper.fetch_chapter(book_id, chapter_id) print(f"\n[章节] {chapter['title']}") print(f" 正文长度: {len(chapter['content'])} 字符") print(f" 正文开头: {chapter['content'][:40]!r}") print(f" 上一章: {chapter['prev']['title'] if chapter['prev'] else None}") print(f" 下一章: {chapter['next']['title'] if chapter['next'] else None}") print("\n验证通过 ✓") return 0 except FetchError as exc: print(f"\n网络错误: {exc.message} (url={exc.url})", file=sys.stderr) print("提示: biquge.tw 可能当前网络不可达,请更换网络后重试。", file=sys.stderr) return 2 except NotFoundError as exc: print(f"\n目标不存在: {exc}", file=sys.stderr) return 3 except ScraperError as exc: print(f"\n解析失败: {exc}", file=sys.stderr) return 4 if __name__ == "__main__": raise SystemExit(main())