{"record":{"id":"1d5cbc712fe2b5b4","repo":"fish2018/pansou","slug":"html-w-1d5cbc","errorCode":null,"errorMessage":"解析HTML失败: %w","messagePattern":"解析HTML失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugin/panwiki/panwiki.go","lineNumber":252,"sourceCode":"\t\treturn nil, fmt.Errorf(\"创建搜索请求失败: %w\", err)\n\t}\n\t\n\tp.setRequestHeaders(req2)\n\t\n\tresp2, err := client.Do(req2)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"搜索请求失败: %w\", err)\n\t}\n\tdefer resp2.Body.Close()\n\t\n\tif resp2.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"搜索请求返回状态码: %d\", resp2.StatusCode)\n\t}\n\t\n\t// 解析搜索结果\n\tdoc, err := goquery.NewDocumentFromReader(resp2.Body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"解析HTML失败: %w\", err)\n\t}\n\t\n\treturn p.extractSearchResults(doc), nil\n}\n\n// setRequestHeaders 设置请求头\nfunc (p *PanwikiPlugin) setRequestHeaders(req *http.Request) {\n\treq.Header.Set(\"User-Agent\", UserAgent)\n\treq.Header.Set(\"Referer\", p.currentBaseURL+\"/\")\n\treq.Header.Set(\"Accept\", \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\")\n\treq.Header.Set(\"Accept-Language\", \"zh-CN,zh;q=0.9,en;q=0.8\")\n\treq.Header.Set(\"Cache-Control\", \"no-cache\")\n\treq.Header.Set(\"Pragma\", \"no-cache\")\n}\n\n// extractSearchResults 提取搜索结果\nfunc (p *PanwikiPlugin) extractSearchResults(doc *goquery.Document) []model.SearchResult {\n\tvar results []model.SearchResult","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/fish2018/pansou/blob/beaa56133755a548ebc51b090b3816e2ae044aa6/plugin/panwiki/panwiki.go#L234-L270","documentation":"goquery.NewDocumentFromReader failed to parse the response body as HTML. goquery (via x/net/html) rarely errors on arbitrary text, so this usually means the body was not decodable content — e.g. compressed/gzip body read incorrectly, truncated response, or binary data.","triggerScenarios":"goquery.NewDocumentFromReader(resp2.Body) returns a parse error: malformed/truncated HTML stream, charset issues producing invalid tokens, or a non-HTML body (binary) when the server mislabeled content type.","commonSituations":"Server returned gzipped content with an exotic configuration; connection cut mid-body causing truncated markup; custom transport not handling Content-Encoding; upstream returning JSON/binary on this endpoint after a site change.","solutions":["Check the Content-Encoding/Content-Type headers; ensure gzip is handled by the transport (default Go transport handles it).","Log the first bytes of the body on parse failure to see what was actually received.","Retry the request if the body was truncated by a network error.","Update goquery/golang.org/x/net to the latest version for parser fixes.","If charset issues, decode with golang.org/x/net/html/charset before parsing."],"exampleFix":"// before\ndoc, err := goquery.NewDocumentFromReader(resp2.Body)\nif err != nil {\n    return nil, fmt.Errorf(\"解析HTML失败: %w\", err)\n}\n// after\ndoc, err := goquery.NewDocumentFromReader(resp2.Body)\nif err != nil {\n    return nil, fmt.Errorf(\"解析HTML失败: %w\", err) // log Content-Type/Encoding and body head for diagnosis\n}","handlingStrategy":"try-catch","validationCode":"// Go: verify content type before parsing\nct := resp2.Header.Get(\"Content-Type\")\nif !strings.Contains(ct, \"text/html\") {\n    return nil, fmt.Errorf(\"unexpected content type: %s\", ct)\n}","typeGuard":null,"tryCatchPattern":"results, err := plugin.Search(keyword, page)\nif err != nil && strings.Contains(err.Error(), \"解析HTML失败\") {\n    log.Printf(\"HTML parse failed, possibly truncated or non-HTML body: %v\", err)\n    // retry once; then alert that the scraper may need updating\n    return err\n}","preventionTips":["Keep goquery/golang.org/x/net updated.","Let the default transport handle gzip; don't manually set Accept-Encoding.","Read the body fully and log a snippet on parse failure.","Retry on truncation-related parse errors."],"tags":["html","parsing","go","scraping"],"backgroundTag":"invalid-json-response","analyzedSha":"beaa56133755a548ebc51b090b3816e2ae044aa6","analyzedAt":"2026-09-07T00:31:18.025Z","contentChangedAt":"2026-09-07T00:31:18.025Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}