Skip to content Skip to sidebar Skip to footer

Parse Html Into Rails Without New Record Every Time?

I have the following code which is parsing a HTML table as simply as possible. # Timestamp (Column 1 of the table) page = agent.page.search('tbody td:nth-child(1)').each do |item|

Solution 1:

Consider iterating over each row instead of each column.

page = agent.page.search("table tbody tr").eachdo |row|
  time        = row.at("td:nth-child(1)").text.strip
  source      = row.at("td:nth-child(2)").text.strip
  destination = row.at("td:nth-child(3)").text.strip
  duration    = row.at("td:nth-child(4)").text.strip
  Call.create!(:time => time, :source => source, :destination => destination, :duration => duration)
end

Solution 2:

Instead of calling call.create everytime just append all the source to a string and the at the end save the record.

Post a Comment for "Parse Html Into Rails Without New Record Every Time?"