BeautifulSoup第n-的类型返回空列表。 Soup.select()[N-1]将返回的

2019-09-28 10:54发布

我想凑这个页面

我的汤选择是:

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

这应返回#CMW-内容文本的第4子表。

但它返回一个空列表。

但是,如果我查询:

test = soup.select('#bodyContent > #mw-content-text > table.wikitable')[3]

我得到了相同的选择。

那我在我的执行缺少什么?

Answer 1:

这是因为发生不能使用nth-of-type()与入级标签,它只能在这样的类型的元件中使用: table:nth-of-type(4) 对于这种特殊的情况下

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

是不可能的,所以你应该使用的解决办法,你在你的问题建议

test = soup.select('#bodyContent > #mw-content-text > table.wikitable')[3]

还检查了这个伟大的问题及后续的答案如何使用:nth-of-type()的CSS3。



文章来源: BeautifulSoup nth-of-type returns empty list. Soup.select()[n -1] returns the elements. Why?