beautifulsoupnthoftype返回空列表。汤。选择()[n1]返回元素。为什么?

2024-05-19 00:00:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在刮this page

我的选汤器是:

test = soup.select('#bodyContent > #mw-content-text > table.wikitable:nth-of-type(4)')

这将返回#cmw content text的第4个子表。在

但它返回一个空列表。在

但如果我问:

^{pr2}$

我有同样的选择器。在

我在实现过程中遗漏了什么?在


Tags: oftexttesttypepagetablecontentthis
1条回答
网友
1楼 · 发布于 2024-05-19 00:00:00

发生这种情况是因为不能将nth-of-type()与类标记一起使用,它只能用在如下元素的类型上:table:nth-of-type(4)。对于这个特殊的例子

test = soup.select('#bodyContent > #mw-content-text > table.wikitable:nth-of-type(4)')

不可能,所以您应该使用您在问题中建议的解决方法

^{pr2}$

另请查看this great question and subsequent answer关于在CSS3中使用:nth-of-type()。在

相关问题 更多 >

    热门问题