2

我有一个Nokogiri::HTML文档。它对应于 Wikipedia 文章中的内容,可能如下所示:

James Henry 'Jimmie' Lyons(生于伊利诺伊州芝加哥- 1892 年 11 月 6 日 - 1963 年 10 月 10 日)是黑人联盟的棒球运动员。他投球和打外场在 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>标签

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

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

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

4

1 回答 1

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"
于 2013-11-04T17:28:44.023 回答