Skip to content

Commit

Permalink
Add scraping implementation (#4)
Browse files Browse the repository at this point in the history
Issue: #2
  • Loading branch information
felipemfp authored Oct 27, 2018
1 parent d1409b5 commit 52f0b9d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ install: dep-ensure
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOCMD) build -a -installsuffix cgo -ldflags="-w -s" -o ./bin/$(PROJECT) ./src/$(PROJECT)

run:
@GOPATH=$(GOPATH) $(GOCMD) run $(PROJECT_PATH)/main.go
@GOPATH=$(GOPATH) $(GOCMD) run $(PROJECT_PATH)/main.go $(WORD)
42 changes: 41 additions & 1 deletion src/sinonimos/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 44 additions & 2 deletions src/sinonimos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package main

import (
"fmt"
"net/http"
"os"

"github.com/yhat/scrape"
"golang.org/x/net/html"
"golang.org/x/text/encoding/charmap"

cli "github.com/jawher/mow.cli"
)

Expand All @@ -13,12 +18,49 @@ func main() {
app.Spec = "PALAVRA"

var (
word = app.StringsArg("PALAVRA", nil, "Palavra para encontrar sinônimos")
word = app.StringArg("PALAVRA", "", "Palavra para encontrar sinônimos")
)

app.Action = func() {
fmt.Printf("Buscando sinônimos para %s\n", *word)
fmt.Printf("Buscando sinônimos para \"%s\":\n", *word)
err := find(*word)
if err != nil {
fmt.Printf("... falhou (%s)\n", err.Error())
}
}

app.Run(os.Args)
}

func find(word string) error {
resp, err := http.Get(fmt.Sprintf("https://www.sinonimos.com.br/%s/", word))
if err != nil {
return err
}

body := charmap.ISO8859_1.NewDecoder().Reader(resp.Body)
root, err := html.Parse(body)
if err != nil {
return err
}

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

synonyms := scrape.FindAll(meaningSection, scrape.ByClass("sinonimo"))
fmt.Print(" ")
for i, synonym := range synonyms {
fmt.Printf("%s", scrape.Text(synonym))
if i == (len(synonyms) - 1) {
fmt.Print("\n")
} else {
fmt.Print(", ")
}
}
}
}

return nil
}

0 comments on commit 52f0b9d

Please sign in to comment.