简体   繁体   English

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

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

This is the table I have: 这是我的桌子:

<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>

And I am trying with Watir to 'scrape' the values and store them in variables in my application. 我正在尝试与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

(Just puts'd to see whether the method had worked or not, once working I'll actually save them in the model.) (只是看一下该方法是否有效,一旦起作用,我就会将它们实际保存在模型中。)

Unfortunately the above isn't capturing and or storing those values as intended. 不幸的是,以上内容并未按预期捕获和/或存储这些值。 Can you help me see the error of my ways please. 您能帮我看看我的错误吗。

Thankyou. 谢谢。

You try to access the values using String for index value, which should be an integer. 您尝试使用String作为索引值访问值,该值应该为整数。 In any case, the final code should look like this: 无论如何,最终代码应如下所示:

 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

In watir, putting s after an element tag gets all the elements with that tag and putting it on an array. 在watir中,将s放在一个元素标签之后,以获取带有该标签的所有元素并将其放在数组上。

So in your case if you put the command @browser.trs.length the value would be 7, since you have 7 rows in your table. 因此,在您的情况下,如果您输入命令@browser.trs.length该值为7,因为表中有7行。

As for the id, I always use @browser.td(:id=>'id') and it always works, though @browser.td(:id, 'id') works for me too. 至于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.

相关问题 如何使用Ruby on Rails和Watir进行Web Scrape多重JavaScript渲染页面 - How to Web Scrape muliple JavaScript Rendered Pages with Ruby on Rails and Watir 如何从Rails 5应用程序中的表的特定属性列表中获取nil和not nil值的计数? - How do I get the count of nil and not nil values from a list of specific attributes from a table in a Rails 5 app? 如何从 Watir cookies 生成 cookie 文件 - How can I generate a cookie file from Watir cookies 如何使用watir从弹出页面访问数据? - How I can access the data from a popup page with watir? 使用Watir和Ruby on Rails,可以通过PC上的模拟器通过Web抓取Android应用程序吗? - Is it possible to web scrape an Android app through an emulator on PC, using Watir with Ruby on Rails? 我可以阻止Rails将实例变量从控制器传递到视图吗? - Can I prevent Rails from passing instance variables from controller to view? 试图通过Watir和Nokogiri刮桌子 - Trying to scrape a table via Watir and Nokogiri 如何从HTML抓取价值? - How can I scrape a value from HTML? 如何从 SQL 返回错误消息并在我的 rails 应用程序中使用这些错误消息? - How can I return error messages from SQL and use these error messages in my rails app? 如何从Rails的“查看”页面中的/ app / assets / javascripts文件夹中调用JS脚本? - How can I call upon a JS script in my /app/assets/javascripts folder from a View page in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM