Skip to content

Instantly share code, notes, and snippets.

@noomerikal
Created December 23, 2011 08:03
Show Gist options
  • Select an option

  • Save noomerikal/bb27cdbf4459fc3b9686 to your computer and use it in GitHub Desktop.

Select an option

Save noomerikal/bb27cdbf4459fc3b9686 to your computer and use it in GitHub Desktop.
mechanize nokogiri pluggable parser
require 'rubygems'
require 'mechanize'
require 'nokogiri'
class NokogiriParser < Mechanize::Page
attr_reader :noko
def initialize uri = nil, response = nil, body = nil, code = nil
@noko = Nokogiri.
super uri, response, body, code
end
end
mech = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
agent.pluggable_parser.html = NokogiriParser
}
mech.get('http://google.com/') do |page|
result = page.form_with(:name => 'f') do |search|
search.q = 'FOO'
end.submit
result.links.each do |link|
puts link.text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment