/
extractor.go
50 lines (37 loc) · 875 Bytes
/
extractor.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
47
48
49
50
package robin
import "github.com/PuerkitoBio/goquery"
type Extractor interface {
Name() string
Extract(sel *goquery.Selection) Items
}
type Extractors map[string]Extractor
type Item map[string]string
type Items []Item
type ItemExtractor struct {
name string
Selector string
DataExtractors DataExtractors
}
func NewItemExtractor(name string, de DataExtractors) *ItemExtractor {
return &ItemExtractor{
name: name,
DataExtractors: de,
}
}
func (e *ItemExtractor) Extract(sel *goquery.Selection) Items {
var items Items
if e.Selector != "" {
sel = sel.Find(e.Selector)
}
sel.Each(func(i int, s *goquery.Selection) {
item := make(Item)
for name, ext := range e.DataExtractors {
item[name] = ext.Extract(s)
}
items = append(items, item)
})
return items
}
func (e *ItemExtractor) Name() string {
return e.name
}