假设我有以下 HTML:
html = Four score and seven <b>years ago</b>
我想用 Hpricot 解析这个:
doc = Hpricot(html)
找到<b>
节点:
node = doc.at('b')
然后<b>
在其父节点中获取节点的字符索引:
node.character_index
=> 22
我该怎么做(即,character_index()
我刚刚编写的函数的真实版本是什么)?
假设我有以下 HTML:
html = Four score and seven <b>years ago</b>
我想用 Hpricot 解析这个:
doc = Hpricot(html)
找到<b>
节点:
node = doc.at('b')
然后<b>
在其父节点中获取节点的字符索引:
node.character_index
=> 22
我该怎么做(即,character_index()
我刚刚编写的函数的真实版本是什么)?
我不认为 Hpricot 那样工作。这是我根据您的示例所做的“node.inspect”
node.inspect
"{elem <b> \"years\" </b>}"
因此,您所要求的整体文本中的位置不存在。
但是,您可能希望将索引用于有限数量的事情,并且您可以通过标准 Hpricot 方法来完成这些事情