Advertisement
Guest User

question 4

a guest
Jul 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.08 KB | None | 0 0
  1. require 'nokogiri'
  2.  
  3. html = Nokogiri::HTML(<<-HTML)
  4.   <div class="listing">
  5.     <div class="row">
  6.       <span class="left">Title:</span>
  7.       <span class="right">The Well-Grounded Rubyist</span>
  8.     </div>
  9.     <div class="row">
  10.       <span class="left">Author:</span>
  11.       <span class="right">David A. Black</span>
  12.     </div>
  13.     <div class="row">
  14.       <span class="left">Price:</span>
  15.       <span class="right">$34.99</span>
  16.     </div>
  17.     <div class="row">
  18.       <span class="left">Description:</span>
  19.       <span class="right">A great book for Rubyists</span>
  20.     </div>
  21.     <div class="row">
  22.       <span class="left">Seller:</span>
  23.       <span class="right">Ruby Scholar</span>
  24.     </div>
  25.   </div>
  26. HTML
  27.  
  28. result = {}
  29. rows = html.css(".listing .row")
  30. rows.each do |row|
  31.   key = row.css(".left").text
  32.   value = row.css(".right").text
  33.   result[key] = value
  34. end
  35.  
  36. p result
  37. # => {"Title:"=>"The Well-Grounded Rubyist",
  38. #     "Author:"=>"David A. Black",
  39. #     "Price:"=>"$34.99",
  40. #     "Description:"=>"A great book for Rubyists",
  41. #     "Seller:"=>"Ruby Scholar"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement