简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM