本篇文章說明 goquery 的部分函式,goquery 是 html parser
Installation
1
| go get github.com/PuerkitoBio/goquery
|
html example
1 2 3 4 5 6 7 8 9 10 11
| html := ` <div class="title"> Example </div> <div class="content"> <p>Hello goquery</p> </div> <div class="other"> Other </div> `
|
string 轉換 goquery.Document
1
| doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
goquery.Document.Find
1 2
| doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html)) fmt.Println(doc.Find("div").First().Text())
|
goquery.Document.Filter
1 2 3 4 5
| doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html)) fmt.Println(doc.Find("div"). Filter(".other"). First(). Text())
|
goquery.Document.FilterFunction
1 2 3 4 5 6 7 8
| doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html)) containParagraph := func(i int, s *goquery.Selection) bool { return s.Find("p").Length() != 0 } fmt.Println(doc.Find("div"). FilterFunction(containParagraph). First(). Text())
|
goquery.Document.Each
1 2 3 4 5 6 7 8
| eachFunc := func(i int, s *goquery.Selection) { fmt.Println(s.Text()) } doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html)) doc.Find("div").Each(eachFunc)
|