forked from squat/drae
/
scrape.go
130 lines (113 loc) · 3.31 KB
/
scrape.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
func ScrapeWord(word string) *Entry {
return Scrape("http://lema.rae.es/drae/srv/search?val="+Escape(word), word)
}
func Scrape(url string, word string) *Entry {
r1, err := http.Get(url)
if err != nil {
panic(err)
}
r2 := Solve(r1)
defer r2.Body.Close()
doc, err := goquery.NewDocumentFromResponse(r2)
if err != nil {
panic(err)
}
//Look for definitions.
nodes := doc.Find("body").Children().Filter("div")
//If no definitions were found, there is probably a list of links to definitions.
if nodes.Length() == 0 {
//Choose the link for the word that is not a verb.
doc.Find("li").EachWithBreak(func(i int, s *goquery.Selection) bool {
url, _ = s.Find("a").Attr("href")
if !strings.HasSuffix(s.Find("b").First().Text(), "r") {
return false
}
if !strings.HasSuffix(s.Find("b").First().Text(), "rse") {
return false
}
return true
})
return Scrape("http://lema.rae.es/drae/srv/"+url, word)
}
etymology := strings.TrimSpace(nodes.First().Find("span.a").Text())
defs := []*Definition{}
vars := []*Variation{}
nodes.Each(func(k int, s *goquery.Selection) {
delimiter := s.Children().Filter("p:not([class])").Eq(1)
delimiter.NextAll().EachWithBreak(func(i int, s *goquery.Selection) bool {
//Skip empty P tags.
if s.Children().Length() == 0 {
return true
}
//Skip morfologies.
if s.Has("a[title='MORFOLOGÍA.']").Length() > 0 {
return true
}
//Break when the first variation is reached
if s.HasClass("p") {
return false
}
defs = append(defs, ScrapeDefinition(s))
return true
})
delimiters := s.Find(".p span.k").Parent()
delimiters.Each(func(i int, v *goquery.Selection) {
vars = append(vars, &Variation{Variation: strings.TrimSpace(v.Text())})
i = len(vars) - 1
v.NextAll().EachWithBreak(func(j int, s *goquery.Selection) bool {
//Done with this variation.
if s.HasClass("p") {
return false
}
vars[i].Definitions = append(vars[i].Definitions, ScrapeDefinition(s))
return true
})
})
})
entry := &Entry{
Word: word,
Etymology: etymology,
Definitions: defs,
Variations: vars,
}
return entry
}
func ScrapeDefinition(s *goquery.Selection) *Definition {
category, _ := s.Find("span[title]").First().Attr("title")
return &Definition{
Category: category,
Definition: strings.TrimSpace(s.Find("span.b").Clone().Children().Not("a").Remove().End().End().Text()),
Origin: ScrapeOrigins(s),
Notes: ScrapeNotes(s),
Examples: ScrapeExamples(s),
}
}
func ScrapeOrigins(s *goquery.Selection) []string {
origins := []string{}
s.Find("span.d i span.d[title]").Each(func(i int, s *goquery.Selection) {
origin, _ := s.Attr("title")
origins = append(origins, origin)
})
return origins
}
func ScrapeNotes(s *goquery.Selection) []string {
notes := []string{}
s.Clone().Find("span[title]").First().Remove().End().End().Find("span.d i span.d[title]").Remove().End().Find("span.d[title]").Each(func(i int, s *goquery.Selection) {
note, _ := s.Attr("title")
notes = append(notes, note)
})
return notes
}
func ScrapeExamples(s *goquery.Selection) []string {
examples := []string{}
s.Find("span.h i").Each(func(i int, s *goquery.Selection) {
examples = append(examples, s.Text())
})
return examples
}