Skip to content

Instantly share code, notes, and snippets.

@makevoid
Created December 14, 2012 02:51
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save makevoid/4282237 to your computer and use it in GitHub Desktop.
Save makevoid/4282237 to your computer and use it in GitHub Desktop.
use Mechanize persisting the cookie jar

EptiMecha

example on how to use mechanize with cookie jars, basically:

agent.cookie_jar.save_as "cookies.yml"
agent.cookie_jar.load "cookies.yml"

to try it:

  • download this zip
  • go to http://bitclan.it and create an account
  • put the PASSWORD in the script
  • run it (ruby eptimecha.rb)
require 'mechanize'
LOGIN_URL = "http://bitclan.it/login"
USER = "makevoid"
PASS = File.read(File.expand_path "~/.password").gsub(/33/, '').strip
COOKIE_FILE = "cookies.yml"
agent = Mechanize.new
page = agent.get LOGIN_URL
form = page.forms.first
form.field_with(id: "username").value = USER
form.field_with(id: "password").value = PASS
page = form.submit
agent.cookie_jar.save_as COOKIE_FILE
p page.body.include? "Privato"
agent = Mechanize.new
agent.cookie_jar.load COOKIE_FILE
page = agent.get "http://bitclan.it"
p page.body.include? "Privato"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment