Class: Spider::GoogleDict
- Inherits:
-
Common
- Object
- Common
- Spider::GoogleDict
show all
- Defined in:
- lib/spider_rails/google_dict.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Common
#full_page, #full_site, #full_site_filter, #get, #get_content, #single, #single_filter
Class Method Details
.alias_methods(*args) ⇒ Object
32
33
34
35
36
|
# File 'lib/spider_rails/google_dict.rb', line 32
def alias_methods(*args)
args.each do |arg|
alias_method arg, args.last
end
end
|
Instance Method Details
#element?(selector, &block) ⇒ Boolean
def login(username, password)
element?('a.gbgt#gb_70') { |e| e.click }
@b.text_field(name: 'Email').set username
@b.text_field(name: 'Passwd').set password
element?('input#signIn') { |e| e.click }
end
83
84
85
86
87
88
89
|
# File 'lib/spider_rails/google_dict.rb', line 83
def element?(selector, &block)
e = @page.element(css: selector)
if yield e
else
'element is nil'
end
end
|
#get_card(keyword) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/spider_rails/google_dict.rb', line 39
def get_card keyword
@card = Hash.new
@page = open_browser "https://www.google.com.hk/search?newwindow=1&safe=strict&q=#{keyword}+define&oq=#{keyword}+define"
doc = Nokogiri::HTML.parse @page.html
GoogleDict.alias_methods :card, :voice, :word, :get_content
card(doc, 'li.dct') do |c|
@card[:Word] = keyword.downcase
@card[:Voice] = voice(c, 'h3+.vk_sh')
get_explain(c)
end
@page.close
@card.delete(0)
@card
end
|
#get_cards(keywords) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/spider_rails/google_dict.rb', line 3
def get_cards(keywords)
Headless.new.start
keywords.each do |keyword|
unless Card.find_by_word(keyword)
get_card keyword
save_record(Card, word: @card[:Word],
voice: @card[:Voice],
verb: @card[:Verb],
adj: @card[:Adjective],
noun: @card[:Noun],
pronoun: @card[:Pronoun],
synonyms: @card[:Synonyms],
abbr: @card[:Abbreviation],
prep: @card[:Preposition],
conj: @card[:Conjunction]
)
end
end
end
|
#get_explain(c) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/spider_rails/google_dict.rb', line 59
def get_explain(c)
type_nodes = c.css('div.vk_gy.vk_sh').to_a
content_nodes = c.css('div.vk_gy.vk_sh+div').to_a
type_nodes.each_with_index do |t, i|
table = content_nodes[i]
if table.css('li').count >= 2
fin_content = Array.new
table.css('li').each do |l|
fin_content << l.content
end
else
fin_content = table.content
end
@card[t.text.to_sym] = fin_content
end
end
|
#get_keywords(path) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/spider_rails/google_dict.rb', line 23
def get_keywords(path)
f = File.new(path)
dict = f.read.split(/\W/)
dict.delete("")
dict.uniq!
dict
end
|