Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting second child in beautiful soup with soup.select?

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.

like image 388
Granitosaurus Avatar asked Jul 13 '14 07:07

Granitosaurus


People also ask

How do you click with BeautifulSoup?

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.

What is a tag in BeautifulSoup?

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.

What is the latest version of BeautifulSoup?

The latest Version of Beautifulsoup is v4. 9.3 as of now.


1 Answers

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)")
like image 128
Stunner Avatar answered Oct 14 '22 16:10

Stunner