如何根据<a>标记之前的文本进行匹配?

时间:2019-12-15 01:16:38

标签: html xml xpath

我有以下HTML:

<html>
    <body>
        I do not want this link here <a href="blah">link text</a>
        But I want this one here <a href="blah blah">more link text</a>
    </body>
</html>

我试图仅获取第二个<a>标记,并使用以下内容:

//*[contains(.,"But I want this one here ")]/a

这将返回

<a href="blah">link text</a>
<a href="blah blah">more link text</a>

我只想

<a href="blah blah">more link text</a>

3 个答案:

答案 0 :(得分:1)

获取以下href的{​​{1}}属性值的一种方法是

<a>

其输出为

//text()[contains(.,"But I want this one here ")]/following-sibling::a/@href

要仅检索元素节点,请省略blah blah

答案 1 :(得分:1)

尝试

# Reshape the data from wide to long
library(reshape2)
df_l <- melt(df_c[colnames(df_c) %in% c("cust", "item", "per_c_i", "per_c", "per_s_i")], 
             id.vars = c("cust", "item"))

#plot the data, faceting by 'hierarchy'
ggplot(df_l, aes(x = cust, y = value, fill = item)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  facet_grid(rows = vars(variable))

答案 2 :(得分:1)

此XPath,

//text()[normalize-space()="But I want this one here"]/following-sibling::*[1]

将选择紧跟"But I want this one here"之后的元素,

<a href="blah blah">more link text</a>

根据要求。