如何使用Nokogiri使用CSS选择器逐行解析?

时间:2013-08-04 20:24:04

标签: html ruby parsing nokogiri mechanize

我有一个我要解析的HTML表格。我想向下移动每个<TR>并提取href。 HTML看起来像这样:

table id="classified_table" class="vs-classified-table widget-off top" cellspacing="0" cellpadding="0" border="0">
    <tbody>
    <tr>
    <td id="classified_cell">
    <table class="vs-classified-table widget-off" cellspacing="0" cellpadding="0" border="0">
    <tbody>
    <tr id="vs_classified_73634384" class="classified row1 kiwii-clad-row kiwii-clad-featured">
    <tr id="vs_classified_74530668" class="classified row2 kiwii-clad-row kiwii-clad-featured">
    <tr id="vs_classified_62296263" class="classified row3 kiwii-clad-row kiwii-clad-featured">
    <tr id="vs_classified_62468547" class="classified row4 kiwii-clad-row kiwii-clad-featured">
    <tr id="vs_classified_47122034" class="classified row5 kiwii-clad-row kiwii-clad-featured">
    <tr id="vs_classified_78210646" class="classified row6 kiwii-clad-row">
    <tr id="vs_classified_78207083" class="classified row7 kiwii-clad-row">
    <tr id="vs_classified_69104369" class="classified row8 kiwii-clad-row">
    <tr id="vs_classified_78113204" class="classified row9 kiwii-clad-row">
    <tr id="vs_classified_52761813" class="classified row10 kiwii-clad-row">
    <tr id="vs_classified_78121746" class="classified row11 kiwii-clad-row">
    <tr id="vs_classified_76515548" class="classified row12 kiwii-clad-row">
    <tr id="vs_advert_middle" class="vs-advertisement advertisment-middle-2 vs-adsense-middle-BR-" style="border:none">
    <tr id="vs_classified_34048811" class="classified row13 kiwii-clad-row">

我的Ruby代码如下所示:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

page = Nokogiri::HTML(open('http://servico-informatica.vivanuncios.com/computador+rio-de-janeiro-capital/'))
rows = page.css('tr#vs_classified_73634384.classified td.summary div a#vs-detail-link-1.kiwii-clear-none')
puts rows.text
#this works

rows [1..10].each do |row|
    puts "this isn't working :("

end

第一次打印成功打印第一个<TR>的文本,但puts循环内的each不起作用。

我要抓的页面是:http://servico-informatica.vivanuncios.com/computador+rio-de-janeiro-capital/

2 个答案:

答案 0 :(得分:3)

您只收到一个结果,因为您的css查询使用的是#,这意味着它正在页面上查找唯一元素(Spec)。

因此,您需要修改查询以根据css类查找href。

tr.classified td.summary a.classified-link

<强>更新

上面的css路径将获取所有链接,然后您只需要遍历数组并使用href和文本执行您需要的操作。

require 'rubygems'
require 'nokogiri'
require 'open-uri'

page = Nokogiri::HTML(open('http://servico-informatica.vivanuncios.com/computador+rio-de-janeiro-capital/'))
links = page.css("tr.classified td.summary a.classified-link")

links.map do |link|
  puts link['href']
  puts link.content
end

答案 1 :(得分:1)

我不知道你期望做什么:

rows [1..10].each do |row|
  puts "this isn't working :("
end

但我很确定它不会做你期望它做的事情。这实际上解释为:

rows[1..10].each { ... }

由于rowsNokogiri::XML::NodeSet)只有一个条目,因此从1开始尝试extract a subset会给您一个空NodeSet;这意味着你实际上只是这么说:

some_empty_node_set.each { ... }

并没有任何用处。但是,如果您查看rows中的第一个条目,就会找到您要查找的href

rows[0]['href']
# "http://servico-informatica.vivanuncios.com/..."

您还可以根据品味和适合您的需求来查看rows.attr('href')rows.first['href']