Documentation
¶
Index ¶
- type Config
- type EmptyContentError
- type Engine
- func (e *Engine) CleanFiles() (int, error)
- func (e *Engine) Close() error
- func (e *Engine) Fetch(ctx context.Context, url string) (*FetchResult, error)
- func (e *Engine) FetchWithOpts(ctx context.Context, url string, opts FetchOptions) (*FetchResult, error)
- func (e *Engine) ParsePDFBytes(ctx context.Context, data []byte, name string, opts ...PDFOption) (*PDFResult, error)
- func (e *Engine) ParsePDFFile(ctx context.Context, filePath string, opts ...PDFOption) (*PDFResult, error)
- func (e *Engine) ParsePDFURL(ctx context.Context, url string, opts ...PDFOption) (*PDFResult, error)
- type FetchOptions
- type FetchResult
- type NotFoundError
- type PDFOption
- type PDFParseError
- type PDFResult
- type SSRFError
- type TimeoutError
- type UnsupportedContentTypeError
- type WAFError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ── HTTP ──
UserAgent string // 默认 Chrome 124 UA
ExtraHeaders map[string]string // 额外请求头
Timeout time.Duration // 单次请求超时,默认 15s
MaxRedirects int // 最大重定向次数,默认 5
// ── 代理 ──
ProxyURL string // HTTP 代理地址,如 "http://user:pass@host:port"
UseSystemProxy bool // 自动使用系统代理(读取 HTTP_PROXY/HTTPS_PROXY 环境变量)
// ── 安全 ──
BlockPrivateIP bool // SSRF 防护,默认 true
MaxURLLength int // 默认 2048
MaxBodyBytes int64 // 下载上限,默认 5MB
// ── 内容输出 ──
MaxInlineLines int // Markdown 超过此行数则写文件,默认 100
MaxInlineChars int // Markdown 超过此字符数则写文件,默认 0(不启用)
MaxContentLength int // 输出给 LLM 的内容上限,默认 100000(100KB)
FileOutputDir string // 文件输出目录,默认 os.TempDir()/webfetch/
FileTTL time.Duration // 文件保留时间,默认 24h
// ── 反爬 ──
FastFailWAF bool // 遇 WAF 立即失败,默认 true
MaxRetries int // 最大重试次数,默认 3
// ── PDF ──
PDFMaxPages int // 最大解析页数,默认 200
PDFHeuristic bool // 是否启用启发式结构化,默认 true
}
Config 引擎配置
type EmptyContentError ¶
EmptyContentError 表示页面返回了 HTTP 200 但内容为空或过少 通常是反爬机制返回的空壳页面(JS 渲染、Token 验证等)
func (*EmptyContentError) Error ¶
func (e *EmptyContentError) Error() string
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine 抓取引擎
func (*Engine) FetchWithOpts ¶
func (e *Engine) FetchWithOpts(ctx context.Context, url string, opts FetchOptions) (*FetchResult, error)
FetchWithOpts 带选项的抓取
func (*Engine) ParsePDFBytes ¶
func (e *Engine) ParsePDFBytes(ctx context.Context, data []byte, name string, opts ...PDFOption) (*PDFResult, error)
ParsePDFBytes 解析 PDF 字节流,可选 PDFOption 覆盖默认行为
type FetchOptions ¶
type FetchOptions struct {
MaxInlineLines *int
MaxInlineChars *int
// PDF 内容(application/pdf)解析选项,nil 字段用默认值
// (MaxPages 200 + Heuristic)。PDFPages 非空时按指定页解析,
// 优先于 PDFMaxPages。
PDFPages []int
PDFMaxPages *int
}
FetchOptions 抓取选项(FetchWithOpts 使用)
type FetchResult ¶
type FetchResult struct {
URL string `json:"url"`
FinalURL string `json:"final_url"`
Title string `json:"title"`
StatusCode int `json:"status_code"`
ContentType string `json:"content_type"`
Method string `json:"method"` // "native-http" | "headless" | "direct"
FetchedAt time.Time `json:"fetched_at"`
Elapsed time.Duration `json:"elapsed"`
// 输出二选一
Mode string `json:"mode"` // "inline" | "saved_to_file"
Markdown string `json:"markdown,omitempty"`
FilePath string `json:"file_path,omitempty"`
// saved_to_file 时的元数据
TotalLines int `json:"total_lines,omitempty"`
TotalChars int `json:"total_chars,omitempty"`
Summary string `json:"summary,omitempty"`
AgentHint string `json:"agent_hint,omitempty"`
// PDF 内容(application/pdf)时携带
PageCount int `json:"page_count,omitempty"` // 文档总页数
Pages []int `json:"pages,omitempty"` // 实际提取的页码
// 网页正文元数据与抽取诊断
Byline string `json:"byline,omitempty"`
PublishedTime *time.Time `json:"published_time,omitempty"`
PreBlocksInput int `json:"pre_blocks_input,omitempty"`
PreBlocksRestored int `json:"pre_blocks_restored,omitempty"`
PreBlocksLost int `json:"pre_blocks_lost,omitempty"`
TableHeadersPromoted int `json:"table_headers_promoted,omitempty"`
TitleInjected bool `json:"title_injected,omitempty"`
Error string `json:"error,omitempty"`
ErrorType string `json:"error_type,omitempty"`
}
FetchResult 网页抓取结果
type NotFoundError ¶
NotFoundError 表示 URL 返回 404
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
type PDFOption ¶ added in v0.3.0
type PDFOption func(*parse.PDFOptions)
PDFOption 定制 PDF 解析行为(ParsePDFFile / ParsePDFBytes / ParsePDFURL 的可选参数)。 未指定的字段沿用引擎 Config(PDFMaxPages / PDFHeuristic)。
func WithPDFHeuristic ¶ added in v0.3.0
WithPDFHeuristic 覆盖启发式结构化开关。
func WithPDFMaxPages ¶ added in v0.3.0
WithPDFMaxPages 覆盖引擎默认的最大解析页数(0 表示不截断)。
func WithPDFPages ¶ added in v0.3.0
WithPDFPages 指定解析页码(1-based,自动去重升序、过滤越界页;全部越界时报错)。 设置后优先于 WithPDFMaxPages。
type PDFParseError ¶
PDFParseError 表示 PDF 解析失败
func (*PDFParseError) Error ¶
func (e *PDFParseError) Error() string
type PDFResult ¶
type PDFResult struct {
SourcePath string `json:"source_path"`
Title string `json:"title"`
PageCount int `json:"page_count"` // 文档总页数(不受 Pages/MaxPages 影响)
Pages []int `json:"pages,omitempty"` // 实际提取的页码
Method string `json:"method"` // "go-fitz"
Mode string `json:"mode"`
Markdown string `json:"markdown,omitempty"`
FilePath string `json:"file_path,omitempty"`
TotalLines int `json:"total_lines,omitempty"`
TotalChars int `json:"total_chars,omitempty"`
Summary string `json:"summary,omitempty"`
AgentHint string `json:"agent_hint,omitempty"`
Error string `json:"error,omitempty"`
ErrorType string `json:"error_type,omitempty"`
}
PDFResult PDF 解析结果
type TimeoutError ¶
type TimeoutError struct {
URL string
}
TimeoutError 表示请求超时
func (*TimeoutError) Error ¶
func (e *TimeoutError) Error() string
type UnsupportedContentTypeError ¶
type UnsupportedContentTypeError struct {
ContentType string
}
UnsupportedContentTypeError 表示不支持的 Content-Type
func (*UnsupportedContentTypeError) Error ¶
func (e *UnsupportedContentTypeError) Error() string