繁体   English   中英

Watir如何在Rails应用程序中将此表中的值抓取到实例变量中?

[英]How can i Watir scrape the values from this table into instance variables in my Rails app?

这是我的桌子:

<tbody><tr>
                  <td class="rhs">Number:</td>
                  <td id="number"><strong>2</strong></td>
       </tr>
       <tr>
                  <td class="rhs">Total:</td>
                  <td id="total"><strong>£60,000</strong></td>           
       </tr>
       <tr>
                  <td class="rhs">GrandTotal</td>
                  <td><strong>£200,000</strong></td>           
       </tr>
       <tr>
                  <td class="rhs">Limit:</td>
                  <td><strong>£550,000</strong></td>         
       </tr>
       <tr>
                  <td class="rhs">Frequency:</td>
                  <td><strong>Annually</strong></td>
       </tr>
       <tr>
                       <td class="rhs">Percentage:</td>
                       <td><strong>0%</strong></td>
       </tr>
       <tr class="display-total">
                      <td class="rhs">Year 1:</td>
                      <td><strong>£480.00</strong></td>
       </tr>        
</tbody></table>

我正在尝试与Watir一起“刮擦”值并将它们存储在应用程序的变量中。

def scrape_quote
        puts @quote.number = @browser.td(:id, 'number').text
        @quote.total = @browser.td(:id, 'total').text
        @quote.grand_total= @browser.tr(:index => '3').td(:index => '1').text
        @quote.limit = @browser.tr(:index => '4').td(:index => '1').text
        @quote.frequency = @browser.tr(:index => '5').td(:index => '1').text
        @quote.percentage = @browser.tr(:index => '6').td(:index => '1').text
        @quote.yr1 = @browser.tr(:index => '7').td(:index => '1').text

        puts @quote.number + ' ' +  @quote.total  + ' ' +  @quote.grand_total
         + ' ' + @quote.limit + ' ' +  @quote.frequency + ' ' +  @quote.commission
          + ' ' +  @quote.yr1
end

(只是看一下该方法是否有效,一旦起作用,我就会将它们实际保存在模型中。)

不幸的是,以上内容并未按预期捕获和/或存储这些值。 您能帮我看看我的错误吗。

谢谢。

您尝试使用String作为索引值访问值,该值应该为整数。 无论如何,最终代码应如下所示:

 rows = @b.trs #Retrieve all trs
scraped_values = {} #Creating a dictionary to store scraped values

for row in rows #iterate over trs
  scraped_values[row[1].id] = row[1].text #retrieve the data
end
puts scraped_values

在watir中,将s放在一个元素标签之后,以获取带有该标签的所有元素并将其放在数组上。

因此,在您的情况下,如果您输入命令@browser.trs.length该值为7,因为表中有7行。

至于id,我总是使用@browser.td(:id=>'id') ,尽管@browser.td(:id, 'id')适用于我,但它始终有效。

def scrape_quote
        puts @quote.number = @browser.td(:id=>'number').text
        @quote.total = @browser.td(:id=>'total').text
        @quote.grand_total= @browser.trs[3].tds[1].text
        @quote.limit = @browser.trs[4].tds[1].text
        @quote.frequency = @browser.trs[5].tds[1].text
        @quote.percentage = @browser.trs[6].tds[1].text
        @quote.yr1 = @browser.trs[7].tds[1].text

        puts @quote.number + ' ' +  @quote.total  + ' ' +  @quote.grand_total
         + ' ' + @quote.limit + ' ' +  @quote.frequency + ' ' +  @quote.commission
          + ' ' +  @quote.yr1
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM