-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopedia.go
41 lines (37 loc) · 917 Bytes
/
gopedia.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
package main
import (
"flag"
"fmt"
"strings"
"github.com/gocolly/colly"
)
func main() {
forbidden := []string{".mw-parser-output"}
lang := flag.String("l", "en", "ISO-639-1 Language code.")
paragraphs := flag.Int("p", 3, "Max number of paragraphs.")
help := flag.Bool("h", false, "Print help")
flag.Parse()
query := strings.Join(flag.Args(), "_")
if *help {
flag.PrintDefaults()
}
c := colly.NewCollector()
url := fmt.Sprintf("https://%s.wikipedia.org/wiki/%s", *lang, query)
var times int = 1
c.OnHTML(".mw-parser-output", func(e *colly.HTMLElement) {
fmt.Println(fmt.Sprintf("%s\n", url))
e.ForEach("p", func(i int, elem *colly.HTMLElement) {
text := strings.TrimSpace(elem.Text)
for i := range forbidden {
if strings.Contains(text, forbidden[i]) {
return
}
}
if times <= *paragraphs && text != "" {
fmt.Println(text)
times++
}
})
})
c.Visit(url)
}