Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过xpath选择器从html标记列表中选择特定的html标记_Html_Xpath - Fatal编程技术网

通过xpath选择器从html标记列表中选择特定的html标记

通过xpath选择器从html标记列表中选择特定的html标记,html,xpath,Html,Xpath,我想从以下html代码中获得一些特定信息: <div class="main"> <div class="a"><div><a>linkname1</a></div></div> <!-- I DON'T want get the text of this 'a' tag --> <div class="b">xxx</div> <div

我想从以下html代码中获得一些特定信息:

<div class="main">    
    <div class="a"><div><a>linkname1</a></div></div> <!-- I DON'T want get the text of this 'a' tag --> 
    <div class="b">xxx</div>
    <div class="c">xxx</div>
    <div class="a"><div><a>linkname2</a></div></div> <!-- I want get the text of this 'a' tag --> 
    <div class="a"><div><a>linkname3</a></div></div> <!-- I want get the text of this 'a' tag --> 
    <div class="a"><div><a>linkname4</a></div></div> <!-- I want get the text of this 'a' tag -->  
    <div class="a"><div><a>linkname5</a></div></div> <!-- I want get the text of this 'a' tag --> 
    <div class="d"></div>
    <div class="c">xxx</div>
    <div class="a"><div><a>linkname6</a></div></div> <!-- I DON'T want get the text of this 'a' tag --> 
    <div class="a"><div><a>linkname7</a></div></div> <!-- I DON'T want get the text of this 'a' tag --> 
    <div class="a"><div><a>linkname8</a></div></div> <!-- I DON'T want get the text of this 'a' tag --> 
    <div class="d"></div>
    <div class="c">xxx</div>
    <div class="a"><div><a>linkname9</a></div></div> <!-- I DON'T want get the text of this 'a' tag --> 
    <div class="a"><div><a>linkname10</a></div></div> <!-- I DON'T want get the text of this 'a' tag --> 
</div>

谢谢:)

您可以尝试以下表达式:

/div/div[position() > 3 and position() < 8]/div/a/text()
/div/div[position()>3和position()<8]/div/a/text()

我找到了一个可能的解决方案:)

您的问题是一个集合问题,如本SO答案中所述:

因此,根据您的具体情况,您应该使用如下交叉口:

(: intersection :)
$set1[count(. | $set2) = count($set2)]
div[@class='c'][1]/following-sibling::*[count(. | current()/div[@class='d'][1]/preceding-sibling::*) = count(current()/div[@class='d'][1]/preceding-sibling::*)]
set1应该是
div[@class='c']

set2应该是
div[@class='d']
前面的集合

现在,根据上面的公式将两者结合起来

set1 = "div[@class='c'][1]/following-sibling::*" and
set2 = "div[@class='d'][1]/preceding-sibling::*"
XPath表达式可能如下所示:

(: intersection :)
$set1[count(. | $set2) = count($set2)]
div[@class='c'][1]/following-sibling::*[count(. | current()/div[@class='d'][1]/preceding-sibling::*) = count(current()/div[@class='d'][1]/preceding-sibling::*)]
输出:

linkname2
linkname3
linkname4
linkname5

你试了什么?显示您当前的XPathI尝试了几件事。我最后一次尝试是://div[@class=“main”]/following::div[count(previous::div[@class=“c”])=1]/div/a/text(),但我什么也没有得到:/对不起,我没有说类“a”的div数可能不同,我不能使用像3或8这样的“硬编码”值