css选择器没有给出所需的结果

时间:2018-04-18 04:26:39

标签: html css scrapy css-selectors

我在此页面:https://en.wikipedia.org/wiki/2018_Commonwealth_Games_medal_table

如果我尝试在chrome控制台中使用css选择器:

totalAngle

我按预期获得O / P:$('.wikitable.sortable.plainrowheaders tr[bgcolor],tr[style] td[align=left] a')

然而,当我尝试:

a tag

我得到O / P:$('.wikitable.sortable.plainrowheaders tr[style],tr[bgcolor] td[align=left] a') 这不是我想要的。

在这两种情况下我都需要<tr bgolor='#CCCCFF'>。我可以看到<a> tag的顺序正在发挥重要作用。我可能会在哪里出错?

1 个答案:

答案 0 :(得分:0)

我认为您应该编写类似这样的代码

$('.wikitable.sortable.plainrowheaders tr[style] a, .wikitable.sortable.plainrowheaders tr[bgcolor] td[align=left] a')

你必须写&#34;一个&#34;每一个查询。

您也可以告诉您是否要选择表格中的所有锚点?因为您现在使用的查询,只选择其中一个锚标记。

要选择所有锚标记,请使用以下查询

$('.wikitable.sortable.plainrowheaders tr td[align=left] a')

基本上是您使用的查询

$('.wikitable.sortable.plainrowheaders tr[bgcolor],tr[style] td[align=left] a')

可以解释为2个查询,如下所示

$('.wikitable.sortable.plainrowheaders tr[bgcolor]')

$('tr[style] td[align=left] a')

这就是为什么你得到&lt; tr&gt;标记在第一个和&lt; a&gt;第二个标记