gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:22 26 4
gpt4 key购买 nike

这是我正在解析的 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" 的良好结果。

为什么会这样,if aa = idSongs.textputs link 返回 "" 吗?

最佳答案

要回答所问的问题,链接会返回“”,因为它是一个空的 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

关于ruby - 为什么用 Nokogiri 解析 HTML 返回空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10708435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com