当前位置: 移动技术网 > IT编程>脚本编程>Python > NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.

NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.

2019年02月14日  | 移动技术网IT编程  | 我要评论

王石的小三,欢乐元帅演员表,非诚勿扰 孟雪

soup=beautifulsoup(html.text,'lxml')
#data=soup.select('body > div.main > div.ctr > div > div.newsmcont > p:nth-of-type(3) > img')
#data=soup.select('body > div.main > div.ctr > div > div.newsmcont > p > img')[2]
data=soup.select('body > div.main > div.ctr > div > div.newsmcont > p:nth-child(3) > img')
print(data)

当使用copy selector时,复制的是nth-child,而soup 似乎不支持nth-child,所以会报以下错误:

notimplementederror: only the following pseudo-classes are implemented: nth-of-type.

将nth-child 改为 nth-of-type 就可以了。

或者去掉nth-child,在后面加上[i-1],即[2]。

关于nth-child 和 nth-type,他们都是取父元素下的第n个元素,他们的区别可以通过下面这个例子了解一下:

<div> 
<ul class="demo"> 
<p>zero</p> 
<li>one</li> 
<li>two</li> 
</ul> 
</div>

上面这个例子,.demo li:nth-child(2)选择的是<li>one</li>节点,.demo li:nth-of-type(2)则选择的是<li>two</li>节点。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网