BeautifulSoup类型的nth返回空列表. Soup.select()[n -1]返回元素.为什么? [英] BeautifulSoup nth-of-type returns empty list. Soup.select()[n -1] returns the elements. Why?

查看:556
本文介绍了BeautifulSoup类型的nth返回空列表. Soup.select()[n -1]返回元素.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除此页面

我的汤选择器是:

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

这应该返回#cmw-content-text的第四个子表.

This should return the 4th child table of #cmw-content-text.

但是它返回一个空列表.

But it's returning an empty list.

但是,如果我查询:

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

我确实有相同的选择器.

I do get the same selector.

我在实现过程中缺少什么?

What am I missing in my implementation?

推荐答案

之所以会发生这种情况,是因为您不能将nth-of-type()与分类标签一起使用,只能将其用于类型这样的元素:table:nth-of-type(4).对于这个特定的实例

This is occurring because you can't use nth-of-type() with a classed tag, it can only be used on a type of element like this: table:nth-of-type(4). For this particular instance

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

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

isn't possible, so you should use the workaround you suggested in your question

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

也请查看这个好问题和后续答案关于在CSS3中使用:nth-of-type().

Also check out this great question and subsequent answer about using :nth-of-type() in CSS3.

这篇关于BeautifulSoup类型的nth返回空列表. Soup.select()[n -1]返回元素.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆