ruby多个连接和DNS查询

时间:2012-05-14 00:20:28

标签: ruby dns net-http

http = Net::HTTP.new("hostname", 80)
http.open_timeout = 300
http.read_timeout = 300

pagereq = lambda {
    http.request(Net::HTTP::Get.new(page, {"User-Agent" => "Mozilla/5.0"})).body }

some_conditions.to_a.each do |n|

    page = "startpage"+n.to_s

    pagereq.call.scan(/criteria1/).each do |m|
        page = "/"+m.to_s
        puts pagereq.call.scan(/criteria2/)
    end

end

我使用此模板从网站收集链接或其他内容。它在每个连接上产生DNS解析,但这并不好。

我感兴趣的是。一次解析主机名,建立连接,进行所有操作,关闭连接。

1 个答案:

答案 0 :(得分:0)

Mechanize使用持久的http连接,并且更容易获取链接:

require 'mechanize'
@agent = Mechanize.new
@agent.user_agent = 'Mozilla/5.0'
page = @agent.get 'http://www.google.com/'
puts page.links.map &:href
相关问题