新学 watir 的小白,正常运行一段时间后,就会报错,不用 thread 也一样,请各位大神指导一下
代码如下:
def get_post_link_by_slice(page_slice)
b = Watir::Browser.new :phantomjs
page_slice.each do |page|
┆ b.goto page
┆ links = b.ul(class: "text-list").lis
┆ links.each do |li|
┆ ┆ link = li.a.href
┆ ┆ if link.end_with?('html') && link.include?('trsyw')
┆ ┆ ┆ puts "#{page} #{link}"
┆ ┆ end
┆ end
end
b.close
end
pages = get_pages()
threads = []
pages.each_slice(300) do |s|
threads << Thread.new do
┆ get_post_link_by_slice(s)
end
end
threads.each { |t| t.join }
报错如下:
/Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/protocol.rb:176:in `rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/protocol.rb:154:in `readuntil'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/protocol.rb:164:in `readline'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http/response.rb:40:in `read_status_line'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http/response.rb:29:in `read_new'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1446:in `block in transport_request'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1443:in `catch'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1443:in `transport_request'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1416:in `request'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1409:in `block in request'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:877:in `start'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1407:in `request'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/default.rb:124:in `response_for'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/default.rb:78:in `request'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/common.rb:61:in `call'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/bridge.rb:170:in `execute'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/oss/bridge.rb:581:in `execute'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/oss/bridge.rb:52:in `get'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/common/navigation.rb:32:in `to'
from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.8.4/lib/watir/browser.rb:82:in `goto'
from test.rb:41:in `block in get_post_link_by_slice'
from test.rb:40:in `each'
from test.rb:40:in `get_post_link_by_slice'
from test.rb:60:in `block in <main>'
from test.rb:58:in `each_slice'
from test.rb:58:in `with_index'
from test.rb:58:in `<main>'