0

我有以下html,其中有几个重复的href。如何仅提取唯一链接

<div class="pages">
  <a href="/search_results.aspx?f=Technology&Page=1" class="active">1</a>
  <a href="/search_results.aspx?f=Technology&Page=2">2</a>
  <a href="/search_results.aspx?f=Technology&Page=3">3</a>
  <a href="/search_results.aspx?f=Technology&Page=4">4</a>
  <a href="/search_results.aspx?f=Technology&Page=5">5</a>
  <a href="/search_results.aspx?f=Technology&Page=2">next &rsaquo;</a>
  <a href="/search_results.aspx?f=Technology&Page=6">last &raquo;</a>
</div> 

# p => is the page that has this html
# The below gives 7 as expected. But I don't need next/last links as they are duplicate    
p.css(".pages a").count

#So I tried uniq which obviously didnt work

p.css(".pages").css("a").uniq            #=> didn't work
p.css(".pages").css("a").to_a.uniq       #=> didn't work
4

3 回答 3

4

尝试从匹配元素 ( ) 中提取“href”属性el.attr('href')

html = Nokogiri::HTML(your_html_string)
html.css('a').map { |el| el.attr('href') }.uniq
# /search_results.aspx?f=Technology&Page=1
# /search_results.aspx?f=Technology&Page=2
# /search_results.aspx?f=Technology&Page=3
# /search_results.aspx?f=Technology&Page=4
# /search_results.aspx?f=Technology&Page=5
# /search_results.aspx?f=Technology&Page=6
于 2013-08-27T17:16:37.653 回答
3

同样可以使用#xpath. 我会做如下:

require 'nokogiri'

doc = Nokogiri::HTML::Document.parse <<-HTML
<div class="pages">
  <a href="/search_results.aspx?f=Technology&Page=1" class="active">1</a>
  <a href="/search_results.aspx?f=Technology&Page=2">2</a>
  <a href="/search_results.aspx?f=Technology&Page=3">3</a>
  <a href="/search_results.aspx?f=Technology&Page=4">4</a>
  <a href="/search_results.aspx?f=Technology&Page=5">5</a>
  <a href="/search_results.aspx?f=Technology&Page=2">next &rsaquo;</a>
  <a href="/search_results.aspx?f=Technology&Page=6">last &raquo;</a>
</div> 
HTML

doc.xpath("//a/@href").map(&:to_s).uniq
# => ["/search_results.aspx?f=Technology&Page=1",
#     "/search_results.aspx?f=Technology&Page=2",
#     "/search_results.aspx?f=Technology&Page=3",
#     "/search_results.aspx?f=Technology&Page=4",
#     "/search_results.aspx?f=Technology&Page=5",
#     "/search_results.aspx?f=Technology&Page=6"]
于 2013-08-27T18:17:05.480 回答
0

另一种做同样工作的方法,在xpath表达式本身中处理 uniq 值选择:

require 'nokogiri'

doc = Nokogiri::HTML::Document.parse <<-HTML
<div class="pages">
  <a href="/search_results.aspx?f=Technology&Page=1" class="active">1</a>
  <a href="/search_results.aspx?f=Technology&Page=2">2</a>
  <a href="/search_results.aspx?f=Technology&Page=3">3</a>
  <a href="/search_results.aspx?f=Technology&Page=4">4</a>
  <a href="/search_results.aspx?f=Technology&Page=5">5</a>
  <a href="/search_results.aspx?f=Technology&Page=2">next &rsaquo;</a>
  <a href="/search_results.aspx?f=Technology&Page=6">last &raquo;</a>
</div> 
HTML

doc.xpath("//a[not(@href = preceding-sibling::a/@href)]/@href").map(&:to_s)
# => ["/search_results.aspx?f=Technology&Page=1",
#     "/search_results.aspx?f=Technology&Page=2",
#     "/search_results.aspx?f=Technology&Page=3",
#     "/search_results.aspx?f=Technology&Page=4",
#     "/search_results.aspx?f=Technology&Page=5",
#     "/search_results.aspx?f=Technology&Page=6"]
于 2013-08-27T18:37:59.443 回答