-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
46 lines (43 loc) · 916 Bytes
/
example_test.go
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
35
36
37
38
39
40
41
42
43
44
45
46
package crepe
import (
"encoding/json"
"fmt"
)
func ExampleUnmarshal() {
html := `
<div id="header">
<h1>Employees</h1>
</div>
<table></table>
<table>
<tbody>
<tr data-id="aaaa" role="engineer">
<td>Tony</td>
<td>Male</td>
<td>20</td>
</tr>
<tr data-id="bbbb" role="manager">
<td>Mary</td>
<td>Female</td>
<td>23</td>
</tr>
</tbody>
</table>
`
var data struct {
Title string `crepe:"div#header>h1,text"`
Employees []*struct {
Id string `crepe:"attr=data-id"`
Name string `crepe:"td,:eq(0),text"`
Gender string `crepe:"td,:eq(1),text"`
Age int `crepe:"td,:eq(2),text"`
Role string `crepe:"attr=role"`
} `crepe:"table,:last,tbody>tr"`
}
if err := Unmarshal([]byte(html), &data); err != nil {
fmt.Printf("error: %v\n", err)
return
}
b, _ := json.MarshalIndent(data, "", "\t")
fmt.Printf("result: %s\n", b)
}