Skip to content

类型定义 ​

Result ​

提取结果,包含文本、元数据和媒体信息。

go
type Result struct {
    Text           string        `json:"text"`
    Title          string        `json:"title"`
    Images         []ImageInfo   `json:"images,omitempty"`
    Links          []LinkInfo    `json:"links,omitempty"`
    Videos         []VideoInfo   `json:"videos,omitempty"`
    Audios         []AudioInfo   `json:"audios,omitempty"`
    ProcessingTime time.Duration `json:"-"`       // 处理耗时(不参与标准序列化)
    WordCount      int           `json:"word_count"`
    ReadingTime    time.Duration `json:"-"`       // 预估阅读时间(不参与标准序列化)
}

字段 ​

字段类型说明
Result.Textstring提取的正文纯文本
Result.Titlestring文档标题(<title>,缺失时取首个 <h1>/<h2>)
Result.Images[]ImageInfo按文档顺序排列的图片;PreserveImages=false 时为空
Result.Links[]LinkInfo按文档顺序排列的链接;PreserveLinks=false 时为空
Result.Videos[]VideoInfo提取的视频源;PreserveVideos=false 时为空
Result.Audios[]AudioInfo提取的音频源;PreserveAudios=false 时为空
Result.ProcessingTimetime.Duration本次提取的墙上时间(JSON 输出中为 processing_time_ms)
Result.WordCountintText 的词数(空白分词,CJK 字符逐字计数)
Result.ReadingTimetime.Duration按 WordCount 与 200 词/分钟估算的阅读时间(JSON 输出中为 reading_time_ms)

MarshalJSON ​

自定义 JSON 序列化由 Result.MarshalJSON 实现:ProcessingTime 和 ReadingTime 虽有 json:"-" 标签(标准序列化会跳过),但 Result.MarshalJSON 会将它们转为毫秒数输出。

go
func (r *Result) MarshalJSON() ([]byte, error)

注意:不支持 round-trip

Result 未实现 UnmarshalJSON。如果将 MarshalJSON() 的输出反序列化回 Result,会丢失 ProcessingTime、ReadingTime 等 duration 字段——JSON 输出的键名(processing_time_ms、reading_time_ms)与 struct 字段名不一致,无法对应还原。

这是有意设计:该 JSON 格式面向外部消费(如 API 响应、日志、前端展示),不是为双向序列化准备的。

ImageInfo ​

Result.Images([]ImageInfo)的元素类型。ImageInfo 保存单个 <img> 的提取信息。

go
type ImageInfo struct {
    URL          string `json:"url"`           // 图片地址
    Alt          string `json:"alt"`           // 替代文本
    Title        string `json:"title"`         // 标题
    Width        string `json:"width"`         // 宽度
    Height       string `json:"height"`        // 高度
    IsDecorative bool   `json:"is_decorative"` // 是否为装饰性图片
    Position     int    `json:"position"`      // 在文档中的位置
}

字段 ​

字段类型说明
ImageInfo.URLstring图片地址(src 属性)
ImageInfo.Altstring替代文本(alt 属性)
ImageInfo.Titlestring咨询标题(title 属性)
ImageInfo.Widthstring宽度属性原文(未解析为数字)
ImageInfo.Heightstring高度属性原文(未解析为数字)
ImageInfo.IsDecorativeboolAlt 为空时为 true(装饰性图片)
ImageInfo.Positionint在提取内容中的 1-based 序号(未定位时为 0)

字段语义 ​

字段说明
URL图片的 src 属性值;仅包含合法 URL(内部校验,不导出),无效 URL 的 <img> 不会出现在结果中
Altalt 属性原文;为空时 IsDecorative 为 true
Titletitle 属性原文(非页面标题)
Width/HeightHTML 属性中的原始字符串(如 "640"、"50%"),未解析为数字——不同页面的写法可能不一致
IsDecorativeAlt 为空时为 true,可用于识别装饰性图片并跳过
Position在文档中的 1-based 序号;当 PreserveImages = false 时整个 Images 切片为空

Width/Height 非数值类型

Width 和 Height 是 string 类型而非 int,保留 HTML 源码中的原始表示(可能含单位、百分比等)。需要数值时由调用方自行解析。

LinkInfo ​

Result.Links([]LinkInfo)的元素类型,保存单个 <a> 的提取信息。

go
type LinkInfo struct {
    URL        string `json:"url"`         // 链接地址
    Text       string `json:"text"`        // 链接文本
    Title      string `json:"title"`       // 链接标题
    IsExternal bool   `json:"is_external"` // 是否为外部链接(基于 URL 本身是否为绝对外部 URL 判定,不与 BaseURL 对比)
    IsNoFollow bool   `json:"is_nofollow"` // 是否为 nofollow
    Position   int    `json:"position"`    // 在文档中的位置
}

字段 ​

字段类型说明
LinkInfo.URLstring链接地址(href 属性)
LinkInfo.Textstring链接可见文本(含嵌套标签内的文本)
LinkInfo.Titlestring咨询标题(title 属性,非链接文本)
LinkInfo.IsExternalbool是否为外部链接
LinkInfo.IsNoFollowboolrel 含 nofollow 时为 true
LinkInfo.Positionint在提取内容中的 1-based 序号(无效 <a> 仍消耗序号,可能不连续)

字段语义 ​

字段说明
URLhref 属性值;仅包含合法 URL(内部校验,不导出),无效 URL 的 <a> 消耗 Position 但不加入切片
Text<a> 标签内所有文本节点的递归拼接(含嵌套标签中的文本)
Titletitle 属性原文(非链接文本)
IsExternal基于 URL 本身是否为绝对外部地址判定,不与 BaseURL 做域名比较——这与 ExtractAllLinks 中的内外链判定不同
IsNoFollowrel 属性中包含 nofollow(大小写不敏感,ASCII 折叠匹配)时为 true
Position在文档中的 1-based 序号;无效 <a>(href 非法或缺失)仍消耗序号但不加入切片,因此 Position 可能不连续

VideoInfo ​

Result.Videos([]VideoInfo)的元素类型。VideoInfo 保存单个视频源的提取信息。

go
type VideoInfo struct {
    URL      string `json:"url"`      // 视频地址
    Type     string `json:"type"`     // 视频类型
    Poster   string `json:"poster"`   // 封面图地址
    Width    string `json:"width"`    // 宽度
    Height   string `json:"height"`   // 高度
    Duration string `json:"duration"` // 时长
}

字段 ​

字段类型说明
VideoInfo.URLstring视频源地址
VideoInfo.Typestring视频类型(文件容器 MIME 或 "embed",见下方取值规则)
VideoInfo.Posterstring<video> 的封面帧地址(poster 属性)
VideoInfo.Widthstring宽度属性原文(未解析为数字)
VideoInfo.Heightstring高度属性原文(未解析为数字)
VideoInfo.Durationstring时长属性原文(未解析为数字)

Type 字段取值规则 ​

Type 值含义产生场景
"embed"iframe 引用的视频页面YouTube、Vimeo、优酷、Bilibili 等嵌入式播放器
MIME 类型(如 "video/mp4")视频文件容器<source type="video/mp4"> 属性值
空字符串未检测到类型<video src="..."> 直接指定源且无 <source> 子元素

视频提取的三来源

视频提取按原始 HTML 扫描 → DOM 遍历 → 正则兜底三步执行,每步去重。详见 媒体提取实战。

AudioInfo ​

Result.Audios([]AudioInfo)的元素类型。AudioInfo 保存单个音频源的提取信息。

go
type AudioInfo struct {
    URL      string `json:"url"`      // 音频地址
    Type     string `json:"type"`     // 音频类型
    Duration string `json:"duration"` // 时长
}

字段 ​

字段类型说明
AudioInfo.URLstring音频源地址
AudioInfo.Typestring音频类型(文件容器 MIME,见下方取值规则)
AudioInfo.Durationstring时长属性原文(未解析为数字)

Type 字段取值规则 ​

Type 值产生场景
MIME 类型(如 "audio/mpeg")<source type="audio/mpeg"> 属性值
空字符串<audio src="..."> 直接指定源且无 <source> 子元素

.ogg 扩展名的双重性

OGG 容器可承载视频或音频,.ogg URL 会同时出现在 Videos 和 Audios 中。仅音频变体 .oga 只出现在 Audios。

LinkResource ​

链接资源,ExtractAllLinks 系列函数与 GroupLinksByType 的元素类型。

go
type LinkResource struct {
    URL   string // 链接地址(启用 ResolveRelativeURLs 时已基于 BaseURL 解析为绝对地址)
    Title string // 链接标题
    Type  string // 链接类型:"link"、"image"、"video"、"audio"、"css"、"js"、"icon"、"media"
}

字段 ​

字段类型说明
LinkResource.URLstring资源地址;ResolveRelativeURLs=true 时已基于 BaseURL 解析为绝对地址
LinkResource.Titlestring人类可读的资源标签
LinkResource.Typestring资源类型分类(link/image/video/audio/css/js/icon/media)

各 Type 取值对应的 HTML 标签来源与控制开关详见 链接提取。

Statistics ​

处理统计信息,由 Processor.GetStatistics 返回。

go
type Statistics struct {
    TotalProcessed     int64         // 总处理数
    CacheHits          int64         // 缓存命中数
    CacheMisses        int64         // 缓存未命中数
    ErrorCount         int64         // 错误数
    AverageProcessTime time.Duration // 平均处理时间
}

字段 ​

字段类型说明
Statistics.TotalProcessedint64完成且无错误的提取次数(含缓存命中)
Statistics.CacheHitsint64由缓存直接命中的次数
Statistics.CacheMissesint64未命中、需完整处理的次数
Statistics.ErrorCountint64返回错误的提取次数
Statistics.AverageProcessTimetime.Duration每次提取的平均墙上时间

BatchResult ​

批量处理结果,ExtractBatch 系列函数与方法的返回类型。

go
type BatchResult struct {
    Results   []*Result // 提取结果,失败或取消时为 nil
    Errors    []error   // 失败的错误
    Success   int       // 成功数量
    Failed    int       // 失败数量
    Cancelled int       // 取消数量
}

字段 ​

字段类型说明
BatchResult.Results[]*Result与输入一一对应的提取结果,失败/取消项为 nil
BatchResult.Errors[]error与输入一一对应的错误,成功项为 nil
BatchResult.Successint成功项计数
BatchResult.Failedint失败项计数
BatchResult.Cancelledint因 context 取消而未处理的项计数

字段语义、取消行为与输入校验详见 批量处理。

NodeAttr ​

HTML 节点属性,ContentNode.Attrs([]NodeAttr)的元素类型。NodeAttr 仅 Key 与 Value 两个字段。

go
type NodeAttr struct {
    Key   string // 属性名
    Value string // 属性值
}

字段 ​

字段类型说明
NodeAttr.Keystring属性名
NodeAttr.Valuestring属性值