ruby 如何关闭机械化连接
ruby how to close a mechanize connection
我遇到了机械化连接太多的问题,我想知道如何关闭连接,因为我想用代理构建一个抓取工具。
我确实找到了
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 块:
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
我遇到了机械化连接太多的问题,我想知道如何关闭连接,因为我想用代理构建一个抓取工具。
我确实找到了
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 块:
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