From: Paul Mena Date: 2013-03-28T01:44:59+09:00 Subject: Re: Nokogiri help parsing HTML I should probably include the whole revised program for context. The argument is the path to an HTML file that contains relevant text between the body=start and body=end tags. #!/usr/bin/env ruby # some initializations @interesting = false @my_text = "" # read the file between the two "body" tags and stash in "my_text" 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 # puts @haiku_text require "nokogiri" doc = Nokogiri::HTML(@my_text) doc.xpath('//address/following-sibling::p//text()').each {|n| p n} # puts doc begin file = File.open("snippet.txt", "w") file.write(@date_string) file.write(doc) rescue IOError => e #some error occur, dir not writable etc. ensure file.close unless file == nil end -- Posted via http://www.ruby-forum.com/.