从Nokogiri文档中提取不在括号内的链接

时间:2013-11-04 14:19:28

标签: html ruby nokogiri

我有 Nokogiri::HTML 文件。它对应于维基百科文章中的内容,可能如下所示:

  

James Henry'Jimmie'Lyons (生于Chicago, Illinois - 1892年11月6日 - 1963年10月10日)是一名baseball球员在Negro Leagues。他pitched并且玩过outfield并且在1910年到1925年之间。

具有相应的HTML:

<p><b>James Henry 'Jimmie' Lyons</b> (born in <a href="/wiki/Chicago,_Illinois" title="Chicago, Illinois" class="mw-redirect">Chicago, Illinois</a> – November 6, 1892 – October 10, 1963) was a <a href="/wiki/Baseball" title="Baseball">baseball</a> player in the <a href="/wiki/Negro_League_baseball" title="Negro League baseball" class="mw-redirect">Negro Leagues</a>.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span>[</span>5<span>]</span></a></sup> He <a href="/wiki/Pitcher" title="Pitcher">pitched</a> and played <a href="/wiki/Outfielder" title="Outfielder">outfield</a> and between 1910 to 1925.

我想在此文档中提取第一个非括号 href标记的<a>属性的值。 < / p>

在这种情况下,正确的答案是提取"/wiki/Baseball",即第二个链接的href属性,因为第一个链接的href/wiki/Chicago,_Illinois位于括号中。

请注意,<a>标记本身可以在href中包含括号,因此像“从HTML中删除所有括号”这样的天真方法是不正确的。

最好的方法是什么?我很确定我需要使用Nokogiri的SAX解析器,但如果有更简单的方法我会喜欢它。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用第一个链接,其中前面的文本节点具有相同数量的左括号和右括号。

require 'nokogiri'

def first_non_parenthesized_href(html)
    doc = Nokogiri::HTML(html)
    return doc.css('a').find{ |a|
        previous_text = a.xpath('preceding::text()').collect(&:text).join
        previous_text.count('(') == previous_text.count(')')
    }['href']   
end

# Original example
html = %q{<p><b>James Henry 'Jimmie' Lyons</b> (born in <a href="/wiki/Chicago,_Illinois" title="Chicago, Illinois" class="mw-redirect">Chicago, Illinois</a> - November 6, 1892 - October 10, 1963) was a <a href="/wiki/Baseball" title="Baseball">baseball</a> player in the <a href="/wiki/Negro_League_baseball" title="Negro League baseball" class="mw-redirect">Negro Leagues</a>.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span>[</span>5<span>]</span></a></sup> He <a href="/wiki/Pitcher" title="Pitcher">pitched</a> and played <a href="/wiki/Outfielder" title="Outfielder">outfield</a> and between 1910 to 1925.}
puts first_non_parenthesized_href(html)
#=> "/wiki/Baseball"

# Example in comment
html = %q{<p><b>Science</b> (from <a href="/wiki/Latin_language" title="Latin language" class="mw-redirect">Latin</a> <i>scientia</i>, meaning "knowledge"<sup id="cite_ref-OnlineEtDict_1-0" class="reference"><a href="#cite_note-OnlineEtDict-1"><span>[</span>1<span>]</span></a></sup>) is a systematic enterprise that builds and organizes <a href="/wiki/Knowledge" title="Knowledge">knowledge</a> in the form of testable explanations and predictions about the <a href="/wiki/Universe" title="Universe">universe</a>.<sup id="cite_ref-wilson_2-0" class="reference"><a href="#cite_note-wilson-2"><span>[</span>2<span>]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span>[</span>3<span>]</span></a></sup> In an older and closely related meaning, "science" also refers to a body of knowledge itself, of the type that can be rationally explained and reliably applied. A practitioner of science is known as a <a href="/wiki/Scientist" title="Scientist">scientist</a>.</p>}
puts first_non_parenthesized_href(html)
#=> "/wiki/Knowledge"
相关问题