使用 Hpricot 查找节点在其父节点内的字符索引
假设我有以下 HTML:
html = Four score and seven <b>years ago</b>
我想用 Hpricot 解析它:
doc = Hpricot(html)
找到 节点:
node = doc.at('b')
然后获取 节点的字符索引在其父级中:
node.character_index
=> 22
我该如何做到这一点(即我刚刚编写的 character_index()
函数的真实版本是什么)?
Suppose I have the following HTML:
html = Four score and seven <b>years ago</b>
I want to parse this with Hpricot:
doc = Hpricot(html)
Find the <b>
node:
node = doc.at('b')
and then get the character index of the <b>
node within its parent:
node.character_index
=> 22
How can I do this (i.e., what's the real version of the character_index()
function I just made up)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为 Hpricot 是这样工作的。这是我根据您的示例执行“node.inspect”的结果
因此,您要求的整个文本中的位置不存在。
但是,您可能希望使用索引执行的操作数量有限,您可以通过 来完成这些操作标准 Hpricot 方法
I don't think Hpricot works like that. Here is what I get doing a "node.inspect" based on your example
So, the position in the overall text that you are asking for just isn't there.
However, there are limited number of things you'd probably like to use the index for and you may be able to do these through the standard Hpricot methods