Skip to content

Commit

Permalink
Fix scraping implementation (#26)
Browse files Browse the repository at this point in the history
Issue: #25
  • Loading branch information
felipemfp authored Oct 30, 2018
1 parent 11664bb commit 942a898
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/sinonimos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"time"

"golang.org/x/net/html/atom"

"github.com/briandowns/spinner"
"github.com/logrusorgru/aurora"

Expand Down Expand Up @@ -66,12 +68,21 @@ func find(word string) error {
return err
}

synonymMatcher := func(n *html.Node) bool {
if n.DataAtom == atom.A || n.DataAtom == atom.Span {
if n.Parent != nil {
return scrape.Attr(n.Parent, "class") == "sinonimos"
}
}
return false
}

meaningSections := scrape.FindAll(root, scrape.ByClass("s-wrapper"))
for j, meaningSection := range meaningSections {
if meaning, ok := scrape.Find(meaningSection, scrape.ByClass("sentido")); ok {
fmt.Printf("\n> %s\n", aurora.Colorize(scrape.Text(meaning), getColors(j)).Bold())

synonyms := scrape.FindAll(meaningSection, scrape.ByClass("sinonimo"))
synonyms := scrape.FindAll(meaningSection, synonymMatcher)
fmt.Print(" ")
for i, synonym := range synonyms {
fmt.Print(scrape.Text(synonym))
Expand Down

0 comments on commit 942a898

Please sign in to comment.