MCPcopy Create free account
hub / github.com/PuerkitoBio/goquery / ExampleSingle

Function ExampleSingle

example_test.go:85–111  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

83}
84
85func ExampleSingle() {
86 html := `
87<html>
88 <body>
89 <div>1</div>
90 <div>2</div>
91 <div>3</div>
92 </body>
93</html>
94`
95 doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
96 if err != nil {
97 log.Fatal(err)
98 }
99
100 // By default, the selector string selects all matching nodes
101 multiSel := doc.Find("div")
102 fmt.Println(multiSel.Text())
103
104 // Using goquery.Single, only the first match is selected
105 singleSel := doc.FindMatcher(goquery.Single("div"))
106 fmt.Println(singleSel.Text())
107
108 // Output:
109 // 123
110 // 1
111}
112
113// This example shows how to use the goquery.Text function to extract clean,
114// human-readable text from a selection, similar to BeautifulSoup's get_text.

Callers

nothing calls this directly

Calls 5

NewDocumentFromReaderFunction · 0.92
SingleFunction · 0.92
FindMethod · 0.80
TextMethod · 0.80
FindMatcherMethod · 0.80

Tested by

no test coverage detected