如何在野村获得独特的链接

  • 本文关键字:链接 ruby nokogiri
  • 更新时间 :
  • 英文 :


我有下面的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

尝试从匹配元素(el.attr('href'))中提取"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

使用#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"]

执行相同工作的另一种方法,其中在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"]

相关内容

  • 没有找到相关文章