None - class - nth-child(2)

43 views
Skip to first unread message

Oleksandr Dranyi

unread,
Aug 26, 2022, 4:13:29 PM8/26/22
to beautifulsoup
I have HTML and 3 class (product-ico col-xs-12 col-sm-4)

<div class="product-ico col-xs-12 col-sm-4">
    <span>AAAAAAAA</span>
</div>
<div class="product-ico col-xs-12 col-sm-4">
    <span>BBBBBBBBB</span>
</div>
<div class="product-ico col-xs-12 col-sm-4">
    <span>CCCCCCCCC</span>
</div>

I try to do =>
    soup_link.select_one(' .product-ico:nth-child(2) span')

But in result I have None ......  What can i get <span>BBBBBBBBB</span>

Senks

Isaac

unread,
Aug 26, 2022, 4:35:42 PM8/26/22
to beautifulsoup

Hi, I’m the author of SoupSieve, the selector library that BeautifulSoup uses.

I’m not sure what soup_link is when looking at your example, so I don’t have context on what you did before calling select_one. When I do the following it works:

from bs4 import BeautifulSoup

HTML = """
<div class="product-ico col-xs-12 col-sm-4">
    <span>AAAAAAAA</span>
</div>
<div class="product-ico col-xs-12 col-sm-4">
    <span>BBBBBBBBB</span>
</div>
<div class="product-ico col-xs-12 col-sm-4">
    <span>CCCCCCCCC</span>
</div>
"""

soup = BeautifulSoup(HTML, 'html.parser')
print(soup.select_one('.product-ico:nth-child(2) span'))

Results

<span>BBBBBBBBB</span>

Try to narrow things down to the simplest case, it will be easier to identify the issue then.

Reply all
Reply to author
Forward
0 new messages