我对Nokogiri的铁路刮刮新手。我有一个网站,我需要从http://fantasy.premierleague.com/stats/elements/?page=1抓一张桌子,看起来像这样:
<table class="ismTable">
<thead>
<tr>
<th></th>
<th></th>
<th>Player</th>
<th>Team</th>
<th><abbr title="Position">Pos</abbr></th>
<th><abbr title="Teams selected by %">Selected</abbr></th>
<th>Price</th>
<th><abbr title="Gameweek points">GW</abbr></th>
<th><abbr title="Total points">Total</abbr></th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="http://cdn.ismfg.net/static/plfpl/img/shirts/data_view/shirt_7.png" alt="Leicester" title="Leicester" class="ismShirtData"></td>
<td><a href="#163" class="ismInfo ismViewProfile"><img src="http://cdn.ismfg.net/static/plfpl/img/icons/info.png" alt="Player information " width="16" height="16"></a></td>
<td>Mahrez</td>
<td>LEI</td>
<td>MID</td>
<td>61.3%</td>
<td>£6.8</td>
<td>21</td>
<td>119</td>
</tr>
</tbody>
</table>
相关表以&#34; table class =&#34; ismTable&#34;开头,但没有ID。这就是我目前所拥有的:
page = Nokogiri::HTML(open("http://fantasy.premierleague.com/stats/elements/?page=1"))
rows = page.css('tr').map do |row|
row.xpath('./td')
end
我的目标是从表中获取信息,并将其输入到#34; player&#34;的数据库表中,其中包含以下字段:
create_table "players", force: true do |t|
t.string "name"
t.integer "total_points"
t.string "team"
t.text "image_link"
t.integer "gw_points"
t.datetime "created_at"
t.datetime "updated_at"
end
因此,在上面的示例HTML中,我需要创建一个新的播放器条目: 名字 - &gt; Mahrez,团队 - &gt; LEI,gw_points - &gt; 21,total_points - &gt; 119
我知道我需要某种循环,但我很难弄清楚如何访问该表,因为它没有ID。