2019-02-13 Python爬虫问题 NotImplementedError: Only the following pseudo-classes are implemented: nth-of...

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个元素,他们的区别可以通过下面这个例子了解一下:

    zero

  • one
  • two

上面这个例子,.demo li:nth-child(2)选择的是

  • one
  • 节点,.demo li:nth-of-type(2)则选择的是
  • two
  • 节点。

    转载于:https://www.cnblogs.com/theDataDigger/p/10368926.html

    你可能感兴趣的:(2019-02-13 Python爬虫问题 NotImplementedError: Only the following pseudo-classes are implemented: nth-of...)