为什么用Nokogiri解析HTML会返回空白?

时间:2012-05-22 19:06:23

标签: ruby nokogiri

这是我正在解析的HTML:

<div class="audio" id="audio59779184_153635497_-28469067_16663">
  <table width="100%" cellspacing="0" cellpadding="0"><tbody><tr>
<td>
        <a onclick="playAudioNew('59779184_153635497_-28469067_16663')"><div class="play_new" id="play59779184_153635497_-28469067_16663"></div></a>
        <input id="audio_info59779184_153635497_-28469067_16663" type="hidden" value="http://cs5888.userapi.com/u59779184/audio/0fc0fc5d8799.mp3,245">
</td>
      <td class="info">
        <div class="duration fl_r" onmousedown="if (window.audioPlayer) audioPlayer.switchTimeFormat('59779184_153635497_-28469067_16663', event);">4:05</div>
        <div class="audio_title_wrap">
<b><a href="/search?c%5Bsection%5D=audio&amp;c%5Bq%5D=Don+Omar+feat.+Lucenzo+and+Pallada">Don Omar feat. Lucenzo and Pallada</a></b> – <span id="title59779184_153635497_-28469067_16663"> Danza Kuduro (Dj Fleep Mashup)(21.05.12).ılııllı.♫♪Новая Клубная Музыка♫♪.ıllıılı.http://vkontakte.ru/public28469067 </span>
</div>
      </td>

    </tr></tbody></table>
<div class="player_wrap">
    <div class="playline" id="line59779184_153635497_-28469067_16663"><div></div></div>
    <div class="player" id="player59779184_153635497_-28469067_16663" ondragstart="return false;" onselectstart="return false;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0"><tbody><tr id="audio_tr59779184_153635497_-28469067_16663" valign="top">
<td style="padding: 0px; width: 100%; position: relative;">
            <div class="audio_white_line" id="audio_white_line59779184_153635497_-28469067_16663" onmousedown="audioPlayer.prClick(event);"></div>
            <div class="audio_load_line" id="audio_load_line59779184_153635497_-28469067_16663" onmousedown="audioPlayer.prClick(event);"><!-- --></div>
            <div class="audio_progress_line" id="audio_progress_line59779184_153635497_-28469067_16663" onmousedown="audioPlayer.prClick(event);">
              <div class="audio_pr_slider" id="audio_pr_slider59779184_153635497_-28469067_16663"><!-- --></div>
            </div>
          </td>
          <td id="audio_vol59779184_153635497_-28469067_16663" style="position: relative;"></td>
        </tr></tbody></table>
</div>
  </div>
</div>

我正在使用的代码:

require 'watir'
require 'nokogiri'
require 'open-uri'


ff = Watir::Browser.new
ff.goto 'http://vk.com/wall-28469067_16663'
htmlSource = ff.html


doc = Nokogiri::HTML(htmlSource, nil, 'UTF-8')

doc.xpath('//div[@class="audio"]/@id').each do |idSongs|
  divSong = doc.css('div#'+idSongs)
  aa = idSongs.text

  link = doc.xpath("//input[@id='#{aa}']//@value")
  puts link
  puts '========================='
end

ff.close

如果我写:

aa = 'audio_info59779184_153625626_-28469067_16663'

puts link返回"http://cs5333.userapi.com/u14251690/audio/bcf80f297520.mp3,217"的良好结果。

为什么会这样,aa = idSongs.text puts link返回" "

2 个答案:

答案 0 :(得分:2)

要回答问题,链接返回&#34;&#34;,因为它是一个空的NodeSet。换句话说,Nokogiri并没有找到你想要的东西。 NodeSet的行为类似于数组,因此当您尝试puts空数组时,您会得到""

因为它是一个NodeSet,你应该像对待一个数组一样迭代它。 (您的doc.css也是如此,它也会返回一个NodeSet。)

它空洞的原因是因为Nokogiri无法找到你想要的东西。您正在寻找aa的内容:

"audio59779184_153635497_-28469067_16663"

将其替换为"//input[@id='#{aa}']"给出:

"//input[@id='audio59779184_153635497_-28469067_16663']"

但应该是:

"//input[@id='audio_info59779184_153635497_-28469067_16663']"

搜索找到的内容:

 doc.search("//input[@id='audio_info59779184_153635497_-28469067_16663']").size => 1

答案 1 :(得分:0)

简短回答:“为什么,如果aa = idSongs.text确实把链接返回”“?”因为你试图找到一个输入元素,它与你已匹配的div具有相同的dom id,而不存在,因此Nokogiri只给你一个空字符串。

看起来他们在几个地方重复使用音频标识符,因此为了使你的代码更加通用,可能会将其提取出来,然后将你的选择作为你需要访问的内容的前缀......就这样:

doc.xpath('//div[@class="audio"]/@id').each do |idSongs|
  divSong = doc.css('div#'+idSongs)
  aa = idSongs.text

  identifier = (match = aa.match(/^audio(.*)$/)) ? match[1] : ""

  link = doc.xpath("//input[@id='audio_info#{identifier}']//@value")
  puts link
  puts '========================='

  ## now if you want:
  title = doc.xpath("//input[@id='title#{identifier}']//@value")
  puts title
end