【问题标题】:Watir web scraper sometimes works and sometimes doesn'tWatir 网络刮刀有时有效,有时无效
【发布时间】:2016-10-09 06:28:42
【问题描述】:
b = Watir::Browser.new :firefox
url_input = "www.url.com"
b.goto url_input
b.driver.manage.timeouts.implicit_wait = 100

h3s = b.h3s class:"class-name"

for h3 in h3s
    h3.wait_until_present

    as = h3.as

    for a in as
        # puts a.text
        name = h3.text
        puts name
    end
end

该代码有时会打印所有 h3 标签的预期名称,但有时当我从终端运行程序时,它不会打印任何内容,最终会出现如下错误:

“/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/protocol.rb:158:in `rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)”。

当我尝试同时打印 a.text 或尝试通过执行 url = a.attribute_value('href') 获取 a 标签的 url 并打印它时,它永远不会起作用。对我来说,它有时运行有时不运行似乎很奇怪。我怀疑这与时间有关,但我完全不明白 Watir 的时间是如何工作的。

【问题讨论】:

    标签: ruby web-scraping watir


    【解决方案1】:

    所以事实证明这是一个时间问题。最终我解决了这个问题:

    for a in as
        a.wait_until_present
        if a.when_present
            name = a.text
            url = a.attribute_value('href')
        end
    end
    

    这有时有效,有时无效。它在随机位置解析我的数据的过程中停止了几次。唉,不过我最终还是得到了我需要的数据。

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 2017-03-31
      • 2015-02-11
      • 2012-08-02
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多