Skip to content

Finding sibling HTML elements with BeautifulSoup in Python

When you have already located one element in a page, sibling methods let you move sideways from it instead of searching the whole document again.

OUTPUT FORMATSHTMLJSONMarkdown

BeautifulSoup provides four methods for this task: two return a single neighbour and two return every neighbour in one direction.

Choose the method by direction and count

Start from a known element, such as the paragraph identified by id="main" in the accompanying example. The choice depends on direction and quantity:

  • Use find_previous_sibling() when you need only the immediately preceding sibling.
  • Use find_next_sibling() when you need only the immediately following sibling.
  • Use find_all_previous() when you need every preceding sibling, in the order returned by BeautifulSoup.
  • Use find_all_next() when you need every following sibling.

A useful check before choosing is to ask whether the neighbouring content is a single adjacent item or a whole run of items. The singular methods suit adjacent-item logic, such as pairing a heading with the paragraph directly after it. The plural methods suit collecting a sequence, such as every paragraph after a marker element. In the example, the element with id="main" is labelled Main Paragraph, so its previous sibling is Second Paragraph, its next sibling is Fourth Paragraph, all previous siblings are the first two paragraphs, and all next siblings are the final two paragraphs. Note the spelling Fifth Pragaraph in the example markup when matching its text exactly.

Worked example and empty results

The protected Python module code0001 parses the sample HTML with BeautifulSoup, locates the paragraph with id="main", and prints the result of each of the four sibling calls. Run that module as supplied to see the single-neighbour and all-neighbour results side by side. If a singular call returns no element, the located node has no sibling in that direction; if a plural call returns an empty collection, there are no siblings in that direction. That distinction helps separate a missing neighbour from a selector or parsing problem with the starting element itself.