Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

java 爬虫解析问题

网址:

https://gongshang.mingluji.com/beijing/name/华夏风情餐饮管理有限公司

css 1

String LastSecondRegisteredFund=document.selectFirst("li:nth-last-child(2)  span[class='field-item']").ownText();

css 2

String LastSecondRegisteredFund=document.selectFirst("li:nth-child(11)  span[class='field-item']").ownText();

我这个网站用的css 选择器 倒着匹配不到,正的可以匹配到?这是为啥呢?也就是第一个匹配不到,第二个可以
上次倒着匹配还是可以的


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

image.png

能匹配到,只不过它下面也有这种元素。

本来我想加个限定条件就可以了,限制在上面那个框里,如:

fieldset.ad_biger li:nth-last-child(2) span[class='field-item']

然而 css 选择器的 :nth-last-child 只对它前面的元素起作用,也就是只对 li 起作用,也就是说加限定条件并不会改变选中的 child,而是在原来那个 child 之后,再加限定判断,就找不到了。

所以,应该把 nth-last-child 先去掉,找到一个限定集合之后,再去倒着找第二个:

fieldset.ad_biger li span[class='field-item']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...