NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.
程序员文章站
2023-02-21 08:23:33
NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type. ......
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>
节点。