Union find
Union find

Reputation: 8160

BeautifulSoup nth-of-type returns empty list. Soup.select()[n -1] returns the elements. Why?

I'm trying to scrape this page

My Soup selector is:

test = soup.select('#bodyContent > #mw-content-text > table.wikitable:nth-of-type(4)')

This should return the 4th child table of #cmw-content-text.

But it's returning an empty list.

But if I query:

test = soup.select('#bodyContent > #mw-content-text > table.wikitable')[3]

I do get the same selector.

What am I missing in my implementation?

Upvotes: 2

Views: 3618

Answers (1)

wpercy
wpercy

Reputation: 10090

This is occurring because you can't use nth-of-type() with a classed tag, it can only be used on a type of element like this: table:nth-of-type(4). For this particular instance

test = soup.select('#bodyContent > #mw-content-text > table.wikitable:nth-of-type(4)')

isn't possible, so you should use the workaround you suggested in your question

test = soup.select('#bodyContent > #mw-content-text > table.wikitable')[3]

Also check out this great question and subsequent answer about using :nth-of-type() in CSS3.

Upvotes: 11

Related Questions