- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 Nokogiri::HTML 文档。它对应于维基百科文章中的内容,可能如下所示:
James Henry 'Jimmie' Lyons (born in Chicago, Illinois – November 6, 1892 – October 10, 1963) was a baseball player in the Negro Leagues. He pitched and played outfield and between 1910 to 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>
标签本身可以在其 href
中包含括号s,所以像“从 HTML 中去掉所有括号”这样天真的方法是不正确的。
最好的方法是什么?我很确定我将需要使用 Nokogiri 的 SAX 解析器,但如果有更简单的方法我会喜欢的。
最佳答案
您可以尝试使用前面的文本节点具有相同数量的左括号和右括号的第一个链接。
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"
关于html - 从 Nokogiri 文档中提取不在括号内的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19769700/
IntelliJ 有没有办法删除周围的括号、括号、引号等?例如,如果我有: "string" 有没有办法删除匹配的引号并得到这个? string 最佳答案 不是直接的,但以下替换表达式(ctrl+R,
我有一段代码是这样的; var x(10); var i = 3; x(i) = 7 document.write("The stored value is " + x(3) +" 这是我正在阅读的书
括号在sql语句中的作用是什么? 例如,在声明中: 插入 table1 ([columnname1], columnname2) 值 (val1, val2) 另外,如果表名在括号中,它会做什么? 最
为什么在“java”中,当你声明“注释”的“参数”时,必须在参数后面放置“一对括号”,注释在语法上与“接口(interface)”形式“非常不同”,所以为什么这很奇怪语法...我知道这与注释是使用幕后
我正在尝试实现后缀到中缀和中缀到后缀(使用堆栈),一切都很顺利,除了当我从后缀转换时我无法想出如何处理括号的想法。它说我必须使用最少数量的括号。例如: ab+c*da-fb-*+ (a+b)*c+
我有这样的数据: $json_data_array = '[ { "id": 1, "value": "hr@test.com",
我有一个字符串,其中包含数字周围的方括号 []。由于此字符串代表我的 SQL 数据库的列名称,因此我需要删除/替换它们。到目前为止,我通过以下方式进行: if (stringWithBracket.C
这是 index.js 文件的代码快照,它是在新的 phonegap 项目中默认创建的。 var app = { // Application Constructor initiali
您好,先生,我正在通过 url 将数组列表 android 发送到 php,它也成功插入,但是 start[ 和 end ] 这个小括号也插入了,我想删除它 我尝试以下代码.. 请告诉我如何删除括号
我正在尝试将 css 括号括在我的 h2 标题周围(大概 90% 都在那里),但我在解决一些小问题时遇到了麻烦: 1. 右边线的间距有点偏,应该拿过来与支架连接。我该如何调整? 和 2. 通过 bg.
有人能给我一些关于这个问题的提示吗:仅当表达式包含正确闭合的圆括号和大括号并且没有其他字符(甚至空格)时,它才是正确的。例如,() ({} () ({})) 是正确的表达式,而 ({)} 不是正确的表
这怎么让宽度变成 100%? .test { width: (50%;); } 我已经知道如何修复它,使其变为 50%,并且该语句或多或少是多余的,我只想知道为什么会发生这种情况。 编辑:ht
请问python的语法本质上df.head()和df.head有什么区别?我可以解释为前一个是用于调用方法,而后一个只是试图获取DataFrame的属性,即头部?我很困惑为什么有时末尾有括号但有时
我通过C#阅读了一些MSDN文档,发现一段代码可以在字符串构造函数和字符串本身之间使用,就像这样 string[] stringname; 这是什么意思呢? 最佳答案 这只是一个数组声明。这意味着st
是否有人知道在创建 PHP 数组时 [ ] 的含义,以及是否真的需要它。因为从我的角度来看。两种方式都够了 方式一,带括号: $cars[] = array ('expensive' => $BMW,
最近我看到了很多将 SQL 值包含在 {} 中的 PHP/MySQL 问题,例如: SELECT * FROM table WHERE field LIKE '{$value}'; 这是怎么回事?它甚
Pattern pattern = Pattern.compile("([a-zA-Z]+)") Matcher matcher = pattern.matcher("Text"); matcher.
这个问题在这里已经有了答案: Usage of string::c_str on temporary string [duplicate] (2 个答案) 关闭 8 年前。 如果我有一个函数 myf
例如, class BasicTransitionFunction(TransitionFunction[GrammarBasedState]): ... 其中TransitionFunc
这个问题在这里已经有了答案: Is short-circuiting logical operators mandated? And evaluation order? (7 个答案) Safety
我是一名优秀的程序员,十分优秀!