我一直在使用以下脚本来尽早结束基于循环的程序,直到最近才运行良好。我没有改变任何东西,没有错误进行故障排除,但它没有做它应该做的事情。提前感谢任何协助。
require 'nokogiri'
require 'open-uri'
require 'fileutils'
Thread.new do
while line = STDIN.gets
break if line.chomp == 'x'
end
# do something here usually a puts or writing files
#end
exit
end
BASE_URL = 'http://example.com'
BASE_DIR = '/some.php?'
page = Nokogiri::HTML(open(BASE_URL))
last_pg = page.xpath('//*[@id="wgo_stats"]/div/p[1]/a').map {|lastpg| lastpg['href'].split('?')[-1].split('-')[0].to_i}.first
puts "Current Index: #{last_pg}"
for pg_number in 1..last_pg do
puts "Getting #{pg_number}"
page = Nokogiri::HTML(open("#{BASE_URL}#{BASE_DIR}#{pg_number}"))
@results << page.xpath('//*[@id="breadcrumb"]/ul/li[2]/span').text.delete(' ') + ':' + page.xpath('//*[@id="breadcrumb"]/ul/li[2]/span').text.delete(' ')
end