这是我正在解析的 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&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
会返回" "
吗?
为了回答提出的问题,link 返回 ",因为它是一个空的 NodeSet。换句话说,Nokogiri没有找到你要找的东西。NodeSet 的行为类似于数组,因此当您尝试puts
空数组时,您会得到""
.
因为它是一个节点集,所以你应该像迭代数组一样迭代它。(你的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
简短回答:"为什么会这样,如果 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