2

I want to choose all of the targeting items except the first one. I used ': nth-of-type(n+2)', but the system shows the error: Only the following pseudo-classes are implemented: nth-of-type

like this:

data1 = soup1.select('#zoom > div > p:nth-of-type(n + 2)')..

Nidhin Joseph
  • 9,981
  • 4
  • 26
  • 48
Aantar
  • 27
  • 7

1 Answers1

1

The issue is in your version of beautifulsoup, make sure you are using version at least 4.7.1+.

In lower versions more advanced CSS selectors weren't supported.

To get your actual version you can do

import bs4

print(bs4.__version__)

Prints:

4.8.0

Then you can do CSS selector e.g. p:not(:nth-child(1)):

data = '''<div>
  <p>
    1
  </p>
  <p>
    2
  </p>
  <p>
    3
  </p>
</div>'''

soup = bs4.BeautifulSoup(data, 'lxml')
print( soup.select('p:not(:nth-child(1))') )

Prints:

[<p>
    2
  </p>, <p>
    3
  </p>]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Yeah my version is 4.6.0. Could you please tell me the code to update it in terminal? – Aantar Aug 22 '19 at 05:48
  • @JiaqiNie you could do `python -m pip install -U bs4` from terminal. More info here https://stackoverflow.com/questions/2720014/how-to-upgrade-all-python-packages-with-pip – Andrej Kesely Aug 22 '19 at 06:53
  • @JiaqiNie Are you sure you are using the correct version of Python and correct version of `bs4`? Are you using Jupyter notebook or other versions of Python? It seems you have mismatch in used versions of python – Andrej Kesely Aug 22 '19 at 07:14