3

私はルビーの初心者なので、我慢してください。

私はselenium-webdriverとrb-appscriptgemsを使用してウェブスクレイピングを行っています。Webサイトへのナビゲーションは、rbuf_fillメソッドを持つNet::Httpオブジェクトによって駆動されているようです。

次のコードを実行します。

sites = File.open("sites.txt", "r") if File::exists?( "sites.txt" )

    if sites != nil
       while (line = sites.gets)

              driver.switch_to.default_content

          begin
                 driver.navigate.to line

          rescue Exception
                 line = line.split.join("\n")
                 puts line + " caused a timeout."
          end

       end

...

このエラーが発生します:

/opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:134:in `rbuf_fill'
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:116:in `readuntil'
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:126:in `readline'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:2219:in `read_status_line'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:2208:in `read_new'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1191:in `transport_request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1177:in `request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1170:in `block in request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:627:in `start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1168:in `request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/default.rb:73:in `response_for'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/default.rb:41:in `request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/common.rb:34:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:406:in `raw_execute'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:384:in `execute'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:171:in `switchToDefaultContent'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/common/target_locator.rb:68:in `default_content'
from auto.rb:25:in `<main>'

なぜこの例外をキャッチできないのかわかりません。を使用rescue Exception するとすべてをキャッチできますが、ご覧のとおり、スクリプトはまだクラッシュします。

また、タイムアウトを明示的にキャッチする必要があるという情報源も見つかったので、次のことも試しました。

rescue Timeout::Error

運がない。

これについてはどんな助けでも大歓迎です。

Rubyバージョン:ruby 1.9.2p290(2011-07-09リビジョン32553)

OS:MacOS Snow Leopard10.6.864ビット

Selenium Webdriverバージョン:2.2.0

4

1 に答える 1

2

Rubyの標準ライブラリのファイル「timeout.rb」は次のように定義しています。

module Timeout
  # Raised by Timeout#timeout when the block times out.
  class Error < RuntimeError

したがって、必要なのrescueTimeout::Exception、ではなくTimeout::Error、より一般的RuntimeErrorにです。その後、それは動作するはずです。

于 2011-08-01T16:13:57.037 に答える