Crate:indexflow-sitemap
indexflow-sitemap 是使用 Rust 编写的高性能、容错、符合 Google 标准的 XML 和纯文本 sitemap 解析器及递归爬虫。
安装
将 indexflow-sitemap 添加到 Cargo.toml:
[dependencies]
indexflow-sitemap = { version = "0.1.0", path = "crates/indexflow-sitemap" }
Feature Flags
| Feature | Default | Description |
|---|---|---|
gzip |
Enabled | 通过 flate2 启用透明 .xml.gz 解压和炸弹防护。 |
fetch |
Enabled | 通过 reqwest 启用异步 HTTP 下载和递归索引展开。 |
无 HTTP 能力的独立最小模式:
[dependencies]
indexflow-sitemap = { version = "0.1.0", default-features = false, features = ["gzip"] }
核心数据模型
ParsedSitemap 枚举
解析任意原始 sitemap 文档时的主要输出。
pub enum ParsedSitemap {
/// A sitemap index pointing to child sitemaps (<sitemapindex>)
Index { child_urls: Vec<String> },
/// A standard URL set containing page records (<urlset>)
UrlSet { entries: Vec<SitemapUrlEntry> },
/// A line-oriented plaintext URL list (.txt)
PlainText { urls: Vec<String> },
}
SitemapUrlEntry 结构体
表示包含 Google 扩展的单个 <url> 条目。
pub struct SitemapUrlEntry {
pub loc: String,
pub lastmod: Option<DateTime<Utc>>,
pub changefreq: Option<ChangeFreq>,
pub priority: Option<f64>,
pub hreflangs: Vec<HreflangEntry>,
pub images: Vec<SitemapImage>,
pub videos: Vec<SitemapVideo>,
pub news: Option<SitemapNews>,
}
公共 API 参考
1. 内存解析(parse_sitemap)
自动识别输入是 XML 还是纯文本并提取所有实体。
2. 递归 HTTP 展开(SitemapFetcher)
异步抓取根 sitemap URL,递归展开嵌套 <sitemapindex>,隔离分支失败并阻止循环。
3. 透明 Gzip 解压(decode_if_gzipped)
检测 magic bytes(0x1F, 0x8B)或 Unicode BOM,并安全解压 gzip sitemap。
错误处理(SitemapError)
SitemapError 包含 XML、解压、解压炸弹、负载过大、编码、URL、IO、网络、HTTP 状态、最大深度和循环引用错误。
- 解压炸弹:膨胀大小超过
limit时触发。 - 负载过大:流式 HTTP 响应超过
MAX_DOWNLOAD_BYTES(52 MiB)时触发。 - 循环引用:子 sitemap 解析为已访问的规范祖先时触发。