[英]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.