html - 是否可以使用:not() selector to target specific text nodes?

标签 html dom web-scraping css-selectors

考虑以下 HTML:

<div class="status-date">
  <strong>Date Available:</strong> 
  10/05/2016
</div>

我希望 :not()选择器能够定位日期字符串“10/05/2016”,如下所示:

.status-date *:not(strong) {
  text-decoration: underline;
}

两个问题:
1.是:not()选择器能够做到这一点吗?
2. 如果没有,任何 CSS 选择器都可以做到这一点吗?

上下文:这实际上与文本节点的样式无关。我正在做一些网络抓取,我想忽略 <strong>在这种情况下标记。如果是关于样式,我可以定位 div直接覆盖 <strong> 上的样式“取消它”。

更多背景:我可以看到我的天真的尝试没有按预期工作。例如,如以下代码笔所示:http://codepen.io/anon/pen/rWezQK 但我可能误解了有关选择器或我所描述的 DOM 结构的深层内容。

最佳答案

简单的选择器代表元素。对于所有简单选择器都是如此,包括 *:not()。文本包含在元素中,但其本身并不是元素。您将无法仅将文本与任何 CSS 选择器“匹配”,因为就选择器而言,DOM 所谓的文本节点甚至不存在于文档树中。

The specification itself offers only three lines on the :not() selector.

规范中的第一行支持这一点:

The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument. It represents an element that is not represented by its argument.

请注意,它说“它代表一个元素”。

如果您正在进行网页抓取,请考虑 XPath:

//div[contains(concat(' ', @class, ' '), ' status-date ')]/strong/following-sibling::text()

关于html - 是否可以使用:not() selector to target specific text nodes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40569934/

相关文章:

JavaScript效率问题: multiple getElementById() vs getElementsByTagName() and looping through result

javascript - ajax加载后找不到按钮

python - 如何使用 python bs4 抓取包含分页下一个标签的股票数据?

python - 从网页中抓取特定文本

r - 网页抓取(在 R 中?)

javascript - 在单个 div 上启用滚动?

javascript - html5 window.localStorage.getItem Item 获取以开头的键

javascript - DOM 方法和浏览器兼容性站点

html - 良好的表格实践 - 在表格中制作标题的最佳方式?

css - 将图像放在 DIV 中的文本旁边