Search code examples
ruby-on-railsrubymechanizeopen-uri

ruby how to close a mechanize connection


I have the problem with Too many connection on mechanize and I wounder how I close a connection since I want to build a scraper with proxy.

I did find the

agent.shutdown

but for somereason I cant get that to work. any help ?

10.times {

    minion = Mechanize.new { |mech|
        mech.open_timeout   = 15
        mech.read_timeout   = 15

    }

    minion.set_proxy '212.82.126.32', 80


    page = minion.get("http://www.whatsmyip.org/")
    proxy_ip_adress = page.parser.css('#ip').text
    puts proxy_ip_adress
    minion.shutdown

}

Solution

  • I think you'll want to use a Mechanize#start block:

    10.times do
      Mechanize.start do |minion|
        minion.open_timeout   = 15
        minion.read_timeout   = 15
    
        minion.set_proxy '212.82.126.32', 80
    
        page = minion.get("http://www.whatsmyip.org/")
        proxy_ip_adress = page.parser.css('#ip').text
        puts proxy_ip_adress
      end
      # minion definitely doesn't exist anymore
    end