3

这是我正在解析的 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返回" "

4

2 回答 2

2

要回答所提出的问题,链接返回“”,因为它是一个空的 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
于 2012-05-22T20:09:14.127 回答
0

简短回答:“为什么会这样,如果 aa = idSongs.text 确实 puts 链接返回“”?因为你试图找到一个与你已经匹配的 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
于 2012-07-16T23:41:04.093 回答