HTMLタグ間のすべてのテキストを取得する最も効率的な方法は何でしょうか?
<div>
<a> hi </a>
....
htmlタグで囲まれたテキストの束。
doc = Nokogiri::HTML(your_html)
doc.xpath("//text()").to_s
Saxパーサーを使用します。XPathオプションよりもはるかに高速です。
require "nokogiri"
some_html = <<-HTML
<html>
<head>
<title>Title!</title>
</head>
<body>
This is the body!
</body>
</html>
HTML
class TextHandler < Nokogiri::XML::SAX::Document
def initialize
@chunks = []
end
attr_reader :chunks
def cdata_block(string)
characters(string)
end
def characters(string)
@chunks << string.strip if string.strip != ""
end
end
th = TextHandler.new
parser = Nokogiri::HTML::SAX::Parser.new(th)
parser.parse(some_html)
puts th.chunks.inspect
ただ行う:
doc = Nokogiri::HTML(your_html)
doc.xpath("//text()").text
このページの質問divのすべてのテキストを取得する方法は次のとおりです。
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://stackoverflow.com/questions/1512850/grabbing-text-between-all-tags-in-nokogiri"))
puts doc.css("#question").to_s