KC L
KC L

Reputation: 89

Beautiful soup 4: Extract text with no tags

<li class="actualPrice price fakeLink " data-automation="actual-price">
       <span class="visuallyhidden">Hello world</span>
Some text I want to extract
</li>

Here are some HTML. I want to extract the text "Some text I want to extract", and I don't want to extract Hello world.

I have try something like find('span') and use next_sibling but I got None.

for a in soup.find_all('li', 'actualPrice'):
        print a.get_text()

And this give me Hello world and "Some text I want to extract". Is there any methods that extract "Some text I want to extract" only?

Upvotes: 0

Views: 217

Answers (2)

Zroq
Zroq

Reputation: 8392

Just for the sake of another method you can use stripped_strings:

for li in soup.find_all('li', 'actualPrice'):
    _, text_you_want = li.stripped_strings
    print (text_you_want)

Outputs:

Some text I want to extract

Upvotes: 1

akash karothiya
akash karothiya

Reputation: 5950

If you want to extract next element after span tag then you can use .next :

>>> for a in soup.find_all('li', 'actualPrice'):
        print(a.span.next.next)
Some text I want to extract

Upvotes: 1

Related Questions