我要做的是
我有一个代理列表ip|端口
现在Mechanize正在使用这些代理我设置了
open_timeout = 20
read_timeout = 20
我想要它做的是重试,但它会重试与相同的代理,即使我有"代理= @代理.order("RANDOM()")。首先,它不会加载新的代理,而是从缓存
加载。下面是我使用的代码:
begin
proxies = @proxies.order("RANDOM()").first
proxy_ip = proxies.ip
proxy_port = proxies.port
puts proxy_ip
puts proxy_port
agent = Mechanize.new
agent.open_timeout = 20
agent.read_timeout = 20
agent.set_proxy(proxy_ip, proxy_port)
page = agent.get("http://whatismyip.org/"
rescue
puts "oh shit, error"
retry
end
doc = page.parser
ip_ad = doc.css('span').text
puts ip_ad
现在我的问题是我如何使它加载一个新的代理,而不是使用它从缓存
对于从谷歌来的人来说这就是我的解决方法
offset = rand(Proxy.count)
proxies = @proxies.offset(offset).first
我用了偏移,效果很好这是
开始Page = minion.submit(form,button)
rescue Mechanize::ResponseCodeError => e
offset = rand(Proxy.count)
proxies = @proxies.offset(offset).first
proxy_ip = proxies.ip
proxy_port = proxies.port
puts proxy_ip
puts proxy_port
minion.open_timeout = 3.0
minion.read_timeout = 3.0
minion.cookie_jar.clear!
minion.max_history = 0
#minion.set_proxy(proxy_ip, proxy_port)
sleep 5.0
if page.parser.xpath('//h2[contains(@class,"name")]').text == "Ingen träff"
puts "ingen träff"
else
puts "retrying"
retry
end
end
享受男人