如果我有以下HTML结构
<section class="main-gallery homeowner-rating content-block">
<!--content-->
</section>
<section class="homeowner-rating content-block">
<!--content-->
</section>
<section class="homeowner-rating content-block">
<!--content-->
</section>
<section class="homeowner-rating content-block">
<!--content-->
</section>
如何选择除第一个之外的所有homeowner-rating.content-block
类?
为了给出一些上下文,我使用Nokogiri设置了一个简单的屏幕抓取,但它试图从返回空白结果的第一个section类获取信息。
def get_testimonials
url = 'http://www.ratedpeople.com/profile/lcc-building-and-construction'
doc = Nokogiri::HTML.parse(open url)
testimonial_section = doc.css('.homeowner-rating.content-block').each do |t|
title = t.css('h4').text.strip
comments = t.css('q').text.strip
author = t.css('cite').text.strip
end
end
根据您当前的设置,有多种方法:
.homeowner-rating+.homeowner-rating
{
color: red;
}
演示:http://jsfiddle.net/PKEv5/
.homeowner-rating:not(.main-gallery)
{
color: red;
}
演示:http://jsfiddle.net/PKEv5/1/
这只会在主库是节点的第一个子库时起作用:
.homeowner-rating:not(:first-child)
{
color: red;
}
演示:http://jsfiddle.net/PKEv5/2/
使用Nokogiri很容易:
require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse(<<EOT)
<section class="main-gallery homeowner-rating content-block">
<p>1</p>
</section>
<section class="homeowner-rating content-block">
<p>2</p>
</section>
<section class="homeowner-rating content-block">
<p>3</p>
</section>
<section class="homeowner-rating content-block">
<p>4</p>
</section>
EOT
doc.css('.homeowner-rating')[1..-1].map(&:to_html)
# => ["<section class="homeowner-rating content-block">n <p>2</p>n</section>",
# "<section class="homeowner-rating content-block">n <p>3</p>n</section>",
# "<section class="homeowner-rating content-block">n <p>4</p>n</section>"]
Nokogiri的search
, css
和xpath
方法返回NodeSets,其行为类似于数组,因此您可以切片结果以获取块。