From: Paul Mena Date: 2013-03-28T21:07:13+09:00 Subject: Re: Nokogiri help parsing HTML Joel Pearson wrote in post #1103448: > You still have @my_text and @date_string there, which leads me to > suspect that's only the last part of your script. > The example I gave is the entire script... You're right. Here's the whole thing: #!/usr/bin/env ruby # some initializations @interesting = false @my_text = "" # read the file between the two "body" tags fname = ARGV[0] IO.foreach(fname) do |line| if line.match(/body="start"/) @interesting = true end # meanwhile let's grab the date string and process it start_column = 4 end_column = 6 target_range = (start_column-1)..(end_column-1) if line.match(/Date<\/dfn>/) pieces = line.split(" ") @date_string = pieces[target_range].join("-") end if line.match(/body="end"/) @interesting = false end if @interesting @my_text << line end end require "nokogiri" doc = Nokogiri::HTML(@my_text) output = @date_string + $/ doc.xpath('//address/following-sibling::p//text()').each { |line| output << ( line.text.strip + $/ ) } File.write("snippet.txt", output) -- Posted via http://www.ruby-forum.com/.