9

Example HTML

<h2 id="name">
    ABC
    <span class="numbers">123</span>
    <span class="lower">abc</span>
</h2>

I can get the numbers with something like:

soup.select('#name > span.numbers')[0].text

How do I get the text ABC using BeautifulSoup and the select function?

What about in this case?

<div id="name">
    <div id="numbers">123</div> 
    ABC
</div>

1 Answer 1

15

In the first case, get the previous sibling:

soup.select_one('#name > span.numbers').previous_sibling

In the second case, get the next sibling:

soup.select_one('#name > #numbers').next_sibling

Note that I assume that it is intentional that here you have the numbers as an id value and the tag is div instead of span. Hence, I've adjusted the CSS selector.


To cover both cases, you can go to the parent of the tag and find the non-empty text node in a non-recursive mode:

parent = soup.select_one('#name > .numbers,#numbers').parent
print(parent.find(text=lambda text: text and text.strip(), recursive=False).strip())

Note the change in the selector - we are asking to match either numbers id or numbers class.

Though, I have a feeling that this universal solution would not be quite reliable because, for starters, I don't know what your real inputs could be.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, the change in the id and div versus span was intentional. Thanks for noticing! Is there a way to start with the parent as in your last solution and then select directly for the first child in case #1 or for the second child in case #2? I am trying to avoid using find or findAll.
@slaw yeah, sure, you can just use the contents list: tag.contents[0] or tag.contents[1]. Or, go through the tag.children generator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.