goquery Example - Get Simple Text From HTML Page


goquery example: answer to Stack Overflow question: go - How to get simple text from HTML page with goquery?

so31514414.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

import (
	"github.com/PuerkitoBio/goquery"
	"strings"
)

const html = `
<div class="container">
    <div class="row">
      <div class="col-lg-8">
        <p align="justify"><b>Name</b>Priyaka</p>
        <p align="justify"><b>Surname</b>Patil</p>
        <p align="justify"><b>Adress</b><br>India,Kolhapur</p>
        <p align="justify"><b>Hobbies&nbsp;</b><br>Playing</p>
        <p align="justify"><b>Eduction</b><br>12th</p>
        <p align="justify"><b>School</b><br>New Highschool</p>
       </div>
    </div>
</div>
`

func main() {
	doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
	if err != nil {
		panic(err)
	}

	doc.Find(".container").Find("[align=\"justify\"]").Each(func(_ int, s *goquery.Selection) {
		prefix := s.Find("b").Text()
		result := strings.TrimPrefix(s.Text(), prefix)
		println(result)
	})
}

Tested on: Ubuntu Linux 15.10, Go 1.6.1.


References:

[1]go - How to get simple text from HTML page with goquery? - Stack Overflow
[2]github.com/PuerkitoBio/goquery - GoDoc
[3]Posts containing 'goquery' - Stack Overflow