3

我正在使用 Nokogiri 抓取一个如下所示的网站:

<div class="BOX">
  <div class="apple">This is an apple.</div>
  <p>Apple a day, doctor away</p>
</div>

<div class="BOX">
  <div class="iphone">This is an iPhone.</div>
  <div class="android">This is an Android.</div>
  <a href="www.apple.com">Apple home page</a>
  <p>Snoop Lion has both. He's rich.</p>
</div>

我想刮掉“BOX” div 中的所有内容。每个“BOX”都有自己独特的 div 和 HTML 标签,没有明显的模式。我该怎么做?

我的第一次尝试是这样的:

require 'uri-open'
require 'nokogiri'

doc = Nokogiri::HTML(open('http://www.examplesite.com'))
doc.css('BOX').each do |box|
  puts box.content
end

但它什么也不返回。我可以解释一下发生了什么吗?

4

2 回答 2

5

我认为你应该使用#inner_htmlmethod 而不是#content. 尽管您的 CSSclass selector规则是错误的。代码应如下所示:

require 'nokogiri'

doc = Nokogiri::HTML::Document.parse <<-eot
<div class="BOX">
  <div class="apple">This is an apple.</div>
  <p>Apple a day, doctor away</p>
</div>

<div class="BOX">
  <div class="iphone">This is an iPhone.</div>
  <div class="android">This is an Android.</div>
  <a href="www.apple.com">Apple home page</a>
  <p>Snoop Lion has both. Hes rich.</p>
</div>
eot

doc.css('.BOX').each do|n|
   p n.inner_html
end

输出:

  <div class="apple">This is an apple.</div>
  <p>Apple a day, doctor away</p>

  <div class="iphone">This is an iPhone.</div>
  <div class="android">This is an Android.</div>
  <a href="www.apple.com">Apple home page</a>
  <p>Snoop Lion has both. He's rich.</p>

#content将通过删除每个节点内的 html 包装器为您提供所有文本。div见下文:

doc.css('.BOX').each do|n|
   puts n.content
end

输出:

  This is an apple.
  Apple a day, doctor away

  This is an iPhone.
  This is an Android.
  Apple home page
  Snoop Lion has both. He's rich.
于 2013-10-04T15:29:09.403 回答
4

你错过了一个点(.)。

没有点,它匹配一个<BOX>标签。为了匹配一个元素,class="BOX"你应该在它前面加上点。

doc.css('.BOX').each do |box|
  #      ^-- here
  puts box.content
end
于 2013-10-04T15:26:07.087 回答