Skip to content

Instantly share code, notes, and snippets.

@terraboops
Last active May 27, 2020 21:02
Show Gist options
  • Save terraboops/781f68add24819e207c4 to your computer and use it in GitHub Desktop.
Save terraboops/781f68add24819e207c4 to your computer and use it in GitHub Desktop.
Simple Mechanize Scraper

Instructions

  • Put both files into an empty directory
  • Open a shell prompt to the directory with the Gemfile and scraper.rb files.
  • Ensure you have bundler installed: gem install bundler
  • Install the dependencies: bundle install
  • Execute the scraper: ruby scarper.rb
source 'https://rubygems.org'
gem 'mechanize'
require 'mechanize'
require 'io/console'
agent = Mechanize.new
page = agent.get('https://github.com/login')
# GitHub's forms didn't have names, had to look up by their action
# Selects the first form whose action includes "/session" from the page's forms array
form = page.forms.find{|form| form.action.include?("/session")}
puts "Command Line GitHub login!"
puts "Login:"
form.login = gets.chomp
print "Password: "
form.password = STDIN.noecho(&:gets).chomp
page = agent.submit(form)
pp page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment