🔵 🔵 🔵


Primary

၊၊||၊|။

Tag.find_all_previous() ⚬|Documentation|1st|20260122170548-00-⌔

Beautiful Soup Documentation — Beautiful Soup 4.4.0 documentation#find-all-previous-and-find-previous

find_all_previous() and find_previous()

Signature: find_all_previous(name, attrs, string, limit, ﹡﹡kwargs)

Signature: find_previous(name, attrs, string, ﹡﹡kwargs)

These methods use.previous_elements to iterate over the tags and strings that came before it in the document. The find_all_previous() method returns all matches, and find_previous() only returns the first match:

first_link = soup.a
first_link
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
 
first_link.find_all_previous("p")
# [<p class="story">Once upon a time there were three little sisters; ...</p>,
#  <p class="title"><b>The Dormouse's story</b></p>]
 
first_link.find_previous("title")
# <title>The Dormouse's story</title>

The call to find_all_previous("p") found the first paragraph in the document (the one with class=”title”), but it also finds the second paragraph, the

tag that contains the tag we started with. This shouldn’t be too surprising: we’re looking at all the tags that show up earlier in the document than the one we started with. A

tag that contains an tag must have shown up before the tag it contains.

Printed 2026-06-28.

(echo:: @ )

Link to original

Secondary

• • •