我需要解析一个HTML文档来计算Ruby中两个标签(包括属性)和文本的字符数。出于性能原因,我不想使用DOM解析器。我看过Nokogiri的SAX和Reader解析器以及SaxMachine,但似乎都没有为我提供跟踪解析器在输入HTML中的位置的方法。
有没有人知道在Ruby中访问此信息的方法?提前致谢
答案 0 :(得分:5)
输入字符串
html = <<-HTML
<html>
<head>
<title>Title</title>
</head>
<body>
Hello world!
</body>
</html>
HTML
愚蠢的解决方案
原始解决方案,它计算每个字母字符(即。</html>
计数4个字符。)
tag_count = 0
text_count = 0
in_tag = false
html.each_char do |char|
case char
when '<'
in_tag = true
when '>'
in_tag = false
when /\w/
in_tag ? tag_count += 1 : text_count += 1
end
end
puts "Text char count: #{text_count}"
puts "Tag char count: #{tag_count}"
Nokogiri SAX解决方案
这个可以很容易地翻译成另一种语言(例如Java)。
require 'nokogiri'
class HtmlCounter < Nokogiri::XML::SAX::Document
attr_accessor :tag_count, :text_count, :comment_count
def initialize(filtered_tags = [])
@filtered_tags = filtered_tags
end
def start_document
@tag_count = Hash.new(0)
@text_count = Hash.new(0)
@comment_count = 0
@current_tags = []
end
def start_element(name, attrs)
# Keep track of the nesting
@current_tags.push(name)
if should_count?
# Count the end element as well
count_tag(name.length * 2)
count_tag(attrs.flatten.map(&:length).inject(0) {|sum, length| sum + length})
end
end
def end_element(name)
@current_tags.pop
end
def comment(string)
count_comment(string.length) if should_count?
end
def characters(string)
count_text(string.strip.length) if should_count?
end
def should_count?
# Are we in a filtered tag ?
(@current_tags & @filtered_tags).empty?
end
def count_text(count)
@text_count[@current_tags.last] += count
end
def count_tag(count)
@tag_count[@current_tags.last] += count
end
def count_comment(count)
@comment_count[@current_tags.last] += count
end
end
# Don't count things in title tags
counter = HtmlCounter.new(["title"])
parser = Nokogiri::HTML::SAX::Parser.new(counter)
parser.parse(html)
puts "Text char count: #{counter.text_count}"
puts "Tag char count: #{counter.tag_count}"
输出:
Text char count: {"body"=>12}
Tag char count: {"html"=>8, "head"=>8, "body"=>8}
希望这有帮助。