/
extract.go
47 lines (39 loc) · 1022 Bytes
/
extract.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
package scraper
import (
"bytes"
"net/url"
goose "github.com/advancedlogic/GoOse"
"github.com/jlubawy/go-boilerpipe"
"github.com/jlubawy/go-boilerpipe/extractor"
)
// Extract the content of an article
func (article *Article) Extract() error {
content, err := ExtractWithGoOse(article.FeedItem.URL, article.HTML)
// content, err := ExtractWithBoilerpipe(article.FeedItem.URL, article.HTML)
if err != nil {
return err
}
article.FeedItem.Content = content
return nil
}
func ExtractWithGoOse(url string, html string) (string, error) {
g := goose.New()
article, err := g.ExtractFromRawHTML(url, html)
if err != nil {
return "", err
}
return article.CleanedText, nil
}
func ExtractWithBoilerpipe(urlStr string, html string) (string, error) {
reader := bytes.NewReader([]byte(html))
url, err := url.Parse(urlStr)
if err != nil {
return "", err
}
doc, err := boilerpipe.NewDocument(reader, url)
if err != nil {
return "", err
}
extractor.Article().Process(doc)
return doc.Content(), nil
}