Skip to content

Instantly share code, notes, and snippets.

@ssig33
Created January 8, 2013 06:29
Show Gist options
  • Select an option

  • Save ssig33/4481723 to your computer and use it in GitHub Desktop.

Select an option

Save ssig33/4481723 to your computer and use it in GitHub Desktop.
require 'mechanize'
url = 'http://o.x0.com/m/128'
def save url
id = url.split("/").last
alice = Mechanize.new
page = alice.get(url)
after = page.root.xpath("//a[@rel='next']").first['href']
title = page.root.xpath("//a[@class='entry-title']").first['title']
body = page.root.xpath("//*[@id='post-#{id}']/div/div")
open("save/#{id}.txt", 'w'){|f|
f.puts <<EOS
#{title}
#{url}
#{body}
EOS
}
puts "saved #{url}"
after
end
loop{
url = save url
break unless url != nil and url != ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment