gpt4 book ai didi

ruby-on-rails - Ruby Mechanize 屏幕抓取帮助

转载 作者:行者123 更新时间:2023-12-04 16:21:05 25 4
gpt4 key购买 nike

我正在尝试在带有日期的表中刮一行。我只想抓取今天有日期的第三行。

这是我的 Mechanize 代码。我正在尝试选择具有今天日期及其列的列行女巫:

agent.page.search("//td").map(&:text).map(&:strip)

Output:
"11-02-2011", "1", "1", "1", "1", "0", "0,00 DKK", "0,00", "0,00 DKK",
"12-02-2011", "5", "5", "1", "4", "0", "0,00 DKK", "0,00", "0,00 DKK",
"14-02-2011", "1", "3", "1", "1", "0", "0,00 DKK", ",00", "0,00 DKK",
"7", "9", "3", "6", "0", "0,00 DKK", "0,00", "0,00 DKK



我只想刮掉今天日期的第三行。

最佳答案

而不是循环遍历 <td>标签使用 '//td' , 搜索 <tr>标签,只抓取第三个,然后循环遍历 '//td' .

Mechanize 在内部使用 Nokogiri,所以这里是如何在 Nokogiri-ese 中做到的:

html = <<EOT
<table>
<tr><td>11-02-2011</td><td>1</td><td>1</td><td>1</td><td>1</td><td>0</td><td>0,00 DKK</td><td>0,00</td><td>0,00 DKK</td></tr>
<tr><td>12-02-2011</td><td>5</td><td>5</td><td>1</td><td>4</td><td>0</td><td>0,00 DKK</td><td>0,00</td><td>0,00 DKK</td></tr>
<tr><td>14-02-2011</td><td>1</td><td>3</td><td>1</td><td>1</td><td>0</td><td>0,00 DKK</td><td>,00</td><td>0,00 DKK</td></tr>
</table>
EOT

require 'nokogiri'
require 'pp'

doc = Nokogiri::HTML(html)

pp doc.search('//tr')[2].search('td').map{ |n| n.text }

>> ["14-02-2011", "1", "3", "1", "1", "0", "0,00 DKK", ",00", "0,00 DKK"]

使用 .search('//tr')[2].search('td').map{ |n| n.text }附加到 Mechanize 的 agent.page ,像这样:
agent.page.search('//tr')[2].search('td').map{ |n| n.text }

我玩 Mechanize 已经有一段时间了,所以它也可能是 agent.page.parser... .

编辑:

there will come more rows in the table. The row that i want to scrape is always the second last.



将这些信息放入您的原始问题中很重要。您的问题越准确,我们的回答就越准确。

关于ruby-on-rails - Ruby Mechanize 屏幕抓取帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4997877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com