0
我嘗試迭代html節點並從此節點獲取信息。HTMLunit迭代節點
這是HTML例如:
<div class="less1">
<h4>Test name 1</h4>
<div>
<div id="email">[email protected]</div>
<div id="email">[email protected]</div>
<div id="email">[email protected]</div>
</div>
</div>
<div class="less1">
<h4>Test name 2</h4>
<div>
<div id="email">[email protected]</div>
<div id="email">[email protected]</div>
<div id="email">[email protected]</div>
</div>
</div>
<div class="less1">
<h4>Test name 3</h4>
<div>
<div id="email">[email protected]</div>
</div>
</div>
<div class="less1">
<h4>Test name 4</h4>
</div>
這是我的代碼示例。
final List<HtmlListItem> nodes = htmlPage.getByXPath("//*[@class=\"less1\"]");
for (HtmlListItem node: nodes) {
final List<?> divs = node.getByXPath("//h4/text()");
}
「申報單」列出的大小總是4
是否有可能從當前節點只得到1的結果?
是的,你可以做到這一點從每個節點中提取數據,但我希望得到幾乎其他信息在這個div裏面。我更新了html示例。我想從
標記中獲得1個節點文本,並從ID爲「email」的元素中獲取所有電子郵件 – user2264784
我只是看到您沒有指定相對xPath。嘗試使用node.getByXPath(「h4/text()」) – Stondylus
node.getByXPath(「h4/text()」)返回null – user2264784