如何使用 goquery 检索 child 元素的完整 HTML?
How to retrieve full HTML of a child element with goquery?
为什么下面的测试失败了?
func TestGetFirstElementHtml(t *testing.T) {
test := `<speak><p>My paragraph</p></speak>`
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(test))
var childrenHtml []string
doc.Find("speak").Children().Each(func(i int, s *goquery.Selection) {
html, _ := s.Html()
childrenHtml = append(childrenHtml, html)
})
if childrenHtml[0] != "<p>My paragraph</p>" {
t.Fatalf("First element html is not valid: '%s'", childrenHtml[0])
}
}
这是测试结果:
=== FAIL: . TestGetFirstElementHtml (0.00s)
main_test.go:45: First element html is not valid: 'My paragraph'
换句话说,如果我无法预测 child 是哪种 html 元素,我如何检索第一个 child 的完整 HTML?
你要的其实是外HTML,调用goquery.OuterHTML
函数就可以得到。根据文件:
func OuterHtml(s *Selection) (string, error)
OuterHtml returns the outer HTML rendering of the first item in the
selection - that is, the HTML including the first element's tag and
attributes.
Unlike InnerHtml, this is a function and not a method on the
Selection, because this is not a jQuery method (in javascript-land,
this is a property provided by the DOM).
所以只需将行更改为:
html, _ := goquery.OuterHTML(s)
为什么下面的测试失败了?
func TestGetFirstElementHtml(t *testing.T) {
test := `<speak><p>My paragraph</p></speak>`
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(test))
var childrenHtml []string
doc.Find("speak").Children().Each(func(i int, s *goquery.Selection) {
html, _ := s.Html()
childrenHtml = append(childrenHtml, html)
})
if childrenHtml[0] != "<p>My paragraph</p>" {
t.Fatalf("First element html is not valid: '%s'", childrenHtml[0])
}
}
这是测试结果:
=== FAIL: . TestGetFirstElementHtml (0.00s)
main_test.go:45: First element html is not valid: 'My paragraph'
换句话说,如果我无法预测 child 是哪种 html 元素,我如何检索第一个 child 的完整 HTML?
你要的其实是外HTML,调用goquery.OuterHTML
函数就可以得到。根据文件:
func OuterHtml(s *Selection) (string, error)
OuterHtml returns the outer HTML rendering of the first item in the selection - that is, the HTML including the first element's tag and attributes.
Unlike InnerHtml, this is a function and not a method on the Selection, because this is not a jQuery method (in javascript-land, this is a property provided by the DOM).
所以只需将行更改为:
html, _ := goquery.OuterHTML(s)