从标签中提取多个标签
Extract multiple tags from within a tag
提前感谢您提供的任何帮助。我正在尝试使用 HtmlAgilityPack 抓取一些 HTML,但在使用 XPATH 语法时遇到了问题。我正在处理的 HTML 有多个标签,我想访问
中的所有标签。
<p class="row" data-pid="5687754180">
<a href="/bod/5687754180.html" class="i gallery" data-ids="1:00c0c_fapkFsQg3Dx">
<span class="price">00</span>
</a>
<span class="txt">
<span class="pl">
<span class="icon icon-star" role="button">
<span class="screen-reader-text">
<? __("favorite this post") ?>
</span>
</span>
<time datetime="2016-07-17 19:36" title="Sun 17 Jul 07:36:03 PM">Jul 17</time> <a href="/bod/5687754180.html" data-id="5687754180" class="hdrlnk">
<span id="titletextonly">☇☇♔♔♔♔♔1998 Mastercraft Prostar㊣</span>
</a>
</span>
<span class="l2">
<span class="price">00</span>
<span class="pnr">
<span class="px">
<span class="p"> pic</span>
</span>
</span>
</span>
<span class="js-only banish-unbanish">
<span class="banish">
<span class="icon icon-trash" role="button"/>
<span class="screen-reader-text">hide this posting</span>
</span>
<span class="unbanish">
<span class="icon icon-trash red" role="button"/> restore this posting</span>
</span>
</span>
</p>
我的想法是我可以遍历所有 < p > 标签并在每个标签中获取我需要的标签,但效果不是很好。这是我想要得到的:
然后继续下一个
,得到同样的东西。我觉得我越来越接近了,但缺少一些关键的东西。例如,此代码段从每个
中获取 "data-pid",但 "titletextonly" 一遍又一遍地相同。
感谢您提供的任何帮助!!
只要您的 XPath 以 /
开头,它将始终被视为绝对 XPath(换句话说,相对于根文档),忽略当前上下文元素,在本例中由变量 title
。也就是说,SelectSingleNode()
将始终 return 整个文档中与 XPath 参数匹配的第一个元素,而不管上下文元素如何。
要使XPath 相对于context 元素,需要在开头添加一个.
:
var node = title.SelectSingleNode(".//span[@id='titletextonly']");
提前感谢您提供的任何帮助。我正在尝试使用 HtmlAgilityPack 抓取一些 HTML,但在使用 XPATH 语法时遇到了问题。我正在处理的 HTML 有多个标签,我想访问
中的所有标签。
<p class="row" data-pid="5687754180">
<a href="/bod/5687754180.html" class="i gallery" data-ids="1:00c0c_fapkFsQg3Dx">
<span class="price">00</span>
</a>
<span class="txt">
<span class="pl">
<span class="icon icon-star" role="button">
<span class="screen-reader-text">
<? __("favorite this post") ?>
</span>
</span>
<time datetime="2016-07-17 19:36" title="Sun 17 Jul 07:36:03 PM">Jul 17</time> <a href="/bod/5687754180.html" data-id="5687754180" class="hdrlnk">
<span id="titletextonly">☇☇♔♔♔♔♔1998 Mastercraft Prostar㊣</span>
</a>
</span>
<span class="l2">
<span class="price">00</span>
<span class="pnr">
<span class="px">
<span class="p"> pic</span>
</span>
</span>
</span>
<span class="js-only banish-unbanish">
<span class="banish">
<span class="icon icon-trash" role="button"/>
<span class="screen-reader-text">hide this posting</span>
</span>
<span class="unbanish">
<span class="icon icon-trash red" role="button"/> restore this posting</span>
</span>
</span>
</p>
我的想法是我可以遍历所有 < p > 标签并在每个标签中获取我需要的标签,但效果不是很好。这是我想要得到的:
然后继续下一个
,得到同样的东西。我觉得我越来越接近了,但缺少一些关键的东西。例如,此代码段从每个
中获取 "data-pid",但 "titletextonly" 一遍又一遍地相同。
感谢您提供的任何帮助!!
只要您的 XPath 以 /
开头,它将始终被视为绝对 XPath(换句话说,相对于根文档),忽略当前上下文元素,在本例中由变量 title
。也就是说,SelectSingleNode()
将始终 return 整个文档中与 XPath 参数匹配的第一个元素,而不管上下文元素如何。
要使XPath 相对于context 元素,需要在开头添加一个.
:
var node = title.SelectSingleNode(".//span[@id='titletextonly']");