2012-04-06 6 views
0

텍스트 파일에서 URL을 가져오고로드되는지 확인하려면 몇 가지 코드를 작성합니다. 내가 가진 코드는 다음과 같습니다루핑 할 때 오류 받기

execution expired 
/Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/jssh_socket.rb:19:in `const_get': wrong number of arguments (2 for 1) (ArgumentError) 
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/jssh_socket.rb:19:in `js_eval' 
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/firefox.rb:303:in `open_window' 
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/firefox.rb:94:in `get_window_number' 
    from /Library/Ruby/Gems/1.8/gems/firewatir-1.9.4/lib/firewatir/firefox.rb:103:in `goto' 
    from samplecodestack.rb:17 
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout' 
    from samplecodestack.rb:16 
    from samplecodestack.rb:13:in `each_line' 
    from samplecodestack.rb:13 

사람이 어떻게 작동 얻을 알고 : 나는 오류가 발생하지만

require 'rubygems' 
require 'watir' 
require 'timeout' 

Watir::Browser.default = "firefox" 
browser = Watir::Browser.new 

File.open('pl.txt').each_line do |urls| 
    begin 
    Timeout::timeout(10) do 
     browser.goto(urls.chomp) 
     if browser.text.include? "server" 
     puts 'here the page didnt' 
     else 
     puts 'here site was found' 
     File.open('works.txt', 'a') { |f| f.puts urls } 
     end 
    end 
    rescue Timeout::Error => e 
    puts e 
    end 
end 

browser.close 

것은 무엇입니까?

+0

을 당신이 원하는 모든 여전히 유효한 URL의 확인 인 경우, 당신은 아마 인터넷 :: HTTP''사용하고'HEAD' 요청을 발행한다 . 사이트 본문에서 "서버"라는 단어를 확인하면 실제로 제대로로드 된 많은 사이트가 제외됩니다. –

+0

그래, 그 중 일부 사이트를 시도했지만 서버가 다운되고 스크립트가 오류를 던지기 만합니다. –

답변

1

net/http를 사용하고 제한 시간도 처리 할 수 ​​있습니다.

require "net/http" 
require "uri" 
File.open('pl.txt').each_line do |urls| 
    uri = URI.parse(urls.chomp) 
    begin 
     response = Net::HTTP.get_response(uri) 
    rescue Exception=> e 
     puts e.message 
     puts "did not load!" 
    end 
end 

스택 추적에 문제가 있지만 goto 문에있는 것 같습니다.

+0

2/3 (리다이렉션)이면 HTTP 상태 코드를 얻을 수 있기 때문에 http/party 또는 http-party와 같은 URL을 사용하는 URL을 확인하는 것이 훨씬 빠르고 간단합니다. 4xx 또는 5xx라면 어떤 종류의 오류가있을 수 있습니다. Watir은 실제 기능 테스트에 더 적합 할 것입니다. –

+0

이것은 결국 제가 함께했던 것입니다, 고마워요. –

0

execution expiredTimeout::timeout의 블록을 초과 할 때 발생하는 오류입니다. 시간 초과는 전체 블록이 지정된 시간 내에 완료되었는지 확인하는 것입니다. 줄 번호 오류가 주어지면로드되는 URL에 10 초 가깝게 찍은 다음 텍스트 검사가 시간 초과되었습니다.

전체 테스트를 완료하는 데 10 초가 걸리지 않고 페이지로드가 10 초를 초과하는 경우에만 시간 초과가 발생한다고 가정합니다. 그래서 당신은 시간 제한 블록 밖으로 if 문을 이동해야합니다 :

File.open('pl.txt').each_line do |urls| 
    begin 
    Timeout::timeout(10) do 
     browser.goto(urls.chomp) 
    end 
    if browser.text.include? "server" 
     puts 'here the page didnt' 
    else 
     puts 'here site was found' 
     File.open('works.txt', 'a') { |f| f.puts urls } 
    end 
    rescue Timeout::Error => e 
    puts 'here the page took too long to load' 
    puts e 
    end 
end 
+0

고마워 형, 결국 넷/http 일을 갔다.하지만 나에게 한 두 가지를 가르쳐 주었고, 전체 차단을하는 Timeout 일에 대해 알지 못했다. 단지 사이트를 체크 한 것으로 생각했다. –

관련 문제