-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyword.rb
executable file
·55 lines (41 loc) · 1.18 KB
/
keyword.rb
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
#!/usr/bin/ruby
#
# Dependencies:
# - nokogiri
# - open-uri
#
require 'rubygems'
require 'nokogiri'
require 'open-uri'
#====================================
def print_usage
print <<TEXT
Searches HTML of given url for occurences of the keyword
in semantic relevant tags.
usage: #{$0} [URL] [Keyword]
TEXT
exit
end
print_usage if ARGV.count != 2
#====================================
def getOccurences(doc, keyword, selector)
i = 0
doc.css(selector).each do |h|
i = i + 1 if h.content.match(/#{keyword}/mi)
end
i
end
keyword = ARGV[1]
doc = Nokogiri::HTML(open(ARGV[0]))
o_total, o_title, o_h, o_link, o_emstrong = 0, 0, 0, 0, 0
puts "Find tags including keyword (#{keyword}):"
o_title = getOccurences(doc, keyword, "title")
puts " - %-30s %4d times " % ["title" , o_title]
o_h = getOccurences(doc, keyword, "h1,h2,h3,h4,h5,h6")
puts " - %-30s %4d times " % ["h1,h2,h3,h4,h5,h6" , o_h]
o_link = getOccurences(doc, keyword, "a")
puts " - %-30s %4d times " % ["a" , o_link]
o_emstrong = getOccurences(doc, keyword, "strong, em")
puts " - %-30s %4d times " % ["strong, em" , o_emstrong]
o_total = o_title + o_h + o_link + o_emstrong
puts "%-33s %4d times " % ["TOTAL" , o_total]