Skip to content

Instantly share code, notes, and snippets.

@gen1321
Created April 7, 2016 18:47
Show Gist options
  • Save gen1321/cf2a979e7ef858f0aa20a5d9baf6eb49 to your computer and use it in GitHub Desktop.
Save gen1321/cf2a979e7ef858f0aa20a5d9baf6eb49 to your computer and use it in GitHub Desktop.
require 'nokogiri'
t = ('<div class="listing">
<div class="row">
<span class="left">Title:</span>
<span class="right">The Well-Grounded Rubyist</span>
</div>
<div class="row">
<span class="left">Author:</span>
<span class="right">David A. Black</span>
</div>
<div class="row">
<span class="left">Price:</span>
<span class="right">$34.99</span>
</div>
<div class="row">
<span class="left">Description:</span>
<span class="right">A great book for Rubyists</span>
</div>
<div class="row">
<span class="left">Seller:</span>
<span class="right">Ruby Scholar</span>
</div>
</div>
')
doc = Nokogiri::HTML(t)
hash = {}
data = doc.search('span')
(0..data.size - 1).step(2).each do |index|
hash[data[index].text]=data[index+1].text
end
p hash
/home/gen/.rbenv/versions/2.2.2/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/gen/upwork/nokogirrri/test.rb
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment