arrays - Ruby, nokogiri and csv: about different sets of data -


i have this:

require 'nokogiri' require 'open-uri' require 'csv'  fields = ['full name', 'address', 'valid phone', 'e-mail'] availablefields = [] availabledatafields = []  csv.open('myfile4.csv', 'a', {:col_sep => ';', :force_quotes => true}) |csv|     doc = nokogiri::html(open(some_of_my_urls))      #we on right page if have content in tag     if doc.xpath('//div[@class = "pad8"]/h3').length > 0          availablefields << 'full name'         availablefields << doc.xpath('//div[@class = "pad8"]/div[@class = "block pad8"]/table/tr/td[1]').text.gsub(/\n+|\r/, "").strip.split(':')         #we received , parsed available fields on page td[1]         #availablefields = ['full name', 'address', 'e-mail']          availabledatafields << doc.xpath('//div[@class = "pad8"]/h3').text         availabledatafields << doc.xpath('//div[@class = "pad8"]/div[@class = "block pad8"]/table/tr/td[3]').text.gsub(/\n+|\r/, "").strip         #we received , parsed available fields data on page td (td[3])         #availabledatafields = ['john doe', 'new york', 'john.doe@mail.com']      end      #write csv file     csv << fields     csv << availabledatafields  end 

so when open csv file have this:

------------------------------------------------------------------------------------------------------ |     full name    |    address   |         valid phone       |              e-mail                  | | ---------------------------------------------------------------------------------------------------| |     john doe     |   new york   |      john.doe@mail.com    |                                      | ------------------------------------------------------------------------------------------------------ 

instead of this

------------------------------------------------------------------------------------------------------ |     full name    |    address   |         valid phone       |               e-mail                 | | ---------------------------------------------------------------------------------------------------| |     john doe     |   new york   |                           |          john.doe@mail.com           | ------------------------------------------------------------------------------------------------------ 

because of pages (some_of_my_urls) might contain not full of available fields can parse of them page has , know available fields (fields) page might have.

how can fix , how can (possibly) use availablefields it. maybe compare fields , availablefields , based on generate right row of csv data? how?

edit: here html code: case 1, case 2


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -