I have:
<h2 id='names'>Names</h2>
<p>John</p>
<p>Peter</p>
now what's the easiest way to get the Peter here if I have h2 tag already? Now I've tried:
soup.select("#names > p:nth-child(1)")
but here I get nth-child NotImplementedError:
NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.
So I'm not sure what's going on here. The second option was to just get all 'p' tag children and hard select [1] but then there's a danger of index out of range which would require to surround every attempt to get Peter with try/except which is a bit silly.
Any way to select nth-child with soup.select() function?
EDIT: replacing nth-child with nth-of-type seemed to do the trick, so the correct line is:
soup.select("#names > p:nth-of-type(1)")
not sure why it doesn't accept nth-child but it seems that both nth-child and nth-of-type return the same results.
Steps to be followed:Create a function to get the HTML document from the URL using requests. get() method by passing URL to it. Create a Parse Tree object i.e. soup object using of BeautifulSoup() method, passing it HTML document extracted above and Python built-in HTML parser.
Going down. One of the important pieces of element in any piece of HTML document are tags, which may contain other tags/strings (tag's children). Beautiful Soup provides different ways to navigate and iterate over's tag's children.
The latest Version of Beautifulsoup is v4. 9.3 as of now.
Adding your edit as an answer so that it can be more easily found by others:
Use nth-of-type
instead of nth-child
:
soup.select("#names > p:nth-of-type(1)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With