Я пытаюсь создать веб-гусеничный движок и немного впасть в заблуждение. В основном то, что я делаю, это извлечение ссылок с веб-страницы и нажатие каждой ссылки на очередь. Всякий раз, когда хиты переводчика Рубин этот раздел кода:Ruby EOFError с open-uri и loop
links.each do |link|
url_frontier.push(link)
end
Я получаю следующее сообщение об ошибке:
/home/blah/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': end of file reached (EOFError)
Если я закомментируйте выше блок кода я не получаю никаких ошибок. Пожалуйста, любая помощь будет оценена по достоинству. Вот остальная часть кода:
require 'open-uri'
require 'net/http'
require 'uri'
class WebCrawler
def self.Spider(root)
eNDCHARS = %{.,'?!:;}
num_documents = 0
token_list = []
url_repository = Hash.new
url_frontier = Queue.new
url_frontier.push(root.to_s)
while !url_frontier.empty? && num_documents < 10
url = url_frontier.pop
if !url_repository.has_key?(url)
document = open(url)
html = document.read
# extract url's
links = URI.extract(html, ['http']).collect { |u| eNDCHARS.index(u[-1]) ? u.chop : u }
links.each do |link|
url_frontier.push(link)
end
# tokenize
Tokenizer.tokenize(document).each do |word|
token_list.push(IndexStructures::Term.new(word, url))
end
# add to the repository
url_repository[url] = true
num_documents += 1
end
end
# sort by term (primary) and document id (secondary) in reverse to aid in the construction of the inverted index
return num_documents, token_list.sort_by! { |term| [term.term, term.document_id]}.reverse!
end
end