ruby-on-rails - ruby 如何关闭 Mechanize 连接

标签 ruby-on-rails ruby mechanize open-uri

我遇到了 Mechanize 连接过多的问题,我想知道如何关闭连接,因为我想用代理构建一个爬虫。

我找到了

agent.shutdown

但出于某种原因我无法让它工作。有什么帮助吗?

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

}

最佳答案

我想你会想要使用 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

关于ruby-on-rails - ruby 如何关闭 Mechanize 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30266231/

相关文章:

ruby-on-rails - Rails 3 - 时间比较?

ruby-on-rails - Rails 4.2 应用程序不提供 Gzipped Assets

ruby-on-rails - rails 4 : undefined method `where' for Array:

ruby - 换行位置

ruby-on-rails - 更改 bundle 安装 Ruby 版本

python - 使用 Python 的 Mechanize 提交 Html 表单时出现问题

perl - 使用 perl 登录的表单提交

ruby-on-rails - HTML 解析 : How to find the image in the document, 哪个被大多数文本包围?

mysql - 查找 sql/rails 中最受欢迎的日期

ruby - 如何解决由于 Ruby 中的垃圾回收引起的偶发性崩溃