我正在尝试创建一个简单的网络抓取工具,但我遇到了一些麻烦。
网站的结构是这样的:
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-01-18">Sun 01-18-15 09:10 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208362">CYCLONES</a></td>
<td><a href="/facilities/22/teams/210190">TIGERS</a></td>
</tr>
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-01-25">Sun 01-25-15 06:40 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208345">LIONS</a></td>
<td><a href="/facilities/22/teams/208362">CYCLONES</a></td>
</tr>
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-02-01">Sun 02-01-15 12:50 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208362">CYCLONES</a></td>
<td><a href="/facilities/22/teams/210041">CLAY</a></td>
</tr>
我目前拥有的是这个:
games = page.css("td[class='gametime']").map{|game| game.parent.css("a").text}
这将返回一个包含三个元素的字符串数组(在此示例中)。但我试图得到的是一个 2D 数组,例如:
games[0][0] #=> Sun 01-18-15 09:10 PM
games[0][1] #=> CYCLONES
games[0][2] #=> TIGERS
我不想要这个(我目前得到的):
games[0] #=> Sun 01-18-15 09:10 PMCYCLONESTIGERS
实现这一目标的最佳方法是什么?
你很接近:
games = page.css("td.gametime").map { |i| i.parent.css("a").map { |j| j.text } }
对于每个td.gametime
,转到其父级并获取所有a
标签,然后将它们映射到其文本。这将为每个游戏提供一个包含三个值的数组,以及一个页面的数组数组。
我不认为text
会为你制作一个数组。我认为您需要嵌套map
语句:
games = page.css("td[class='gametime']").map{|game| game.parent.css("a").map(&:text)}
我会这样做:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-01-18">Sun 01-18-15 09:10 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208362">CYCLONES</a></td>
<td><a href="/facilities/22/teams/210190">TIGERS</a></td>
</tr>
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-01-25">Sun 01-25-15 06:40 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208345">LIONS</a></td>
<td><a href="/facilities/22/teams/208362">CYCLONES</a></td>
</tr>
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-02-01">Sun 02-01-15 12:50 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208362">CYCLONES</a></td>
<td><a href="/facilities/22/teams/210041">CLAY</a></td>
</tr>
EOT
代码如下:
games = doc.search('tr').map{ |tr| tr.search('td').map(&:text) }
# => [["Sun 01-18-15 09:10 PM", "CYCLONES", "TIGERS"],
# ["Sun 01-25-15 06:40 PM", "LIONS", "CYCLONES"],
# ["Sun 02-01-15 12:50 PM", "CYCLONES", "CLAY"]]
games[0][0] # => "Sun 01-18-15 09:10 PM"
games[0][1] # => "CYCLONES"
games[0][2] # => "TIGERS"
没有必要获取此 HTML 的<td>
标记中的内部标记。有时需要忽略其他文本,这使得它变得必要,但由于它很简单,代码可以很简单。 <td>
节点的text
将返回嵌入在其中的文本节点。
我严重怀疑他们提供的HTML是否那么简单,如果没有更多的细节,我无法给出更准确的答案。(提供足够详细和准确的输入是应该/有益的。不过,一般的想法是找到包含所需行的表,然后向下钻取:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<table class="foo">
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-01-18">Sun 01-18-15 09:10 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208362">CYCLONES</a></td>
<td><a href="/facilities/22/teams/210190">TIGERS</a></td>
</tr>
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-01-25">Sun 01-25-15 06:40 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208345">LIONS</a></td>
<td><a href="/facilities/22/teams/208362">CYCLONES</a></td>
</tr>
<tr>
<td class="gametime"><a href="/facilities/22/games?exact_date=15-02-01">Sun 02-01-15 12:50 PM</a></td>
<td class="gamehome"><a href="/facilities/22/teams/208362">CYCLONES</a></td>
<td><a href="/facilities/22/teams/210041">CLAY</a></td>
</tr>
</table>
<table class="bar">
</table>
EOT
以及修改后的代码:
games = doc.search('table.foo tr').map{ |tr| tr.search('td').map(&:text) }
# => [["Sun 01-18-15 09:10 PM", "CYCLONES", "TIGERS"],
# ["Sun 01-25-15 06:40 PM", "LIONS", "CYCLONES"],
# ["Sun 02-01-15 12:50 PM", "CYCLONES", "CLAY"]]
games[0][0] # => "Sun 01-18-15 09:10 PM"
games[0][1] # => "CYCLONES"
games[0][2] # => "TIGERS"