Beautiful Soup - find_previous_siblings() Method



Method Description

The find_previous_siblings() method in Beautiful Soup package returns all siblings that appear earlier to this PAgeElement in the document and match the given criteria.

Syntax

find_previous_siblings(name, attrs, string, limit, **kwargs)

Parameters

  • name − A filter on tag name.

  • attrs − A dictionary of filters on attribute values.

  • string − A filter for a NavigableString with specific text.

  • limit − Stop looking after finding this many results.

  • kwargs − A dictionary of filters on attribute values.

Return Value

The find_previous_siblings() method a ResultSet of PageElements.

Example 1

Let us use the following HTML snippet for this purpose −

<p>
   <b>
      Excellent
   </b>
   <i>
      Python
   </i>
   <u>
      Tutorial
   </u>
</p>

In the code below, we try to find all the siblings of <> tag. There are two more tags at the same level in the HTML string used for scraping.

from bs4 import BeautifulSoup
soup = BeautifulSoup("<p><b>Excellent</b><i>Python</i><u>Tutorial</u></p>", 'html.parser')

tag1 = soup.find('u')
print ("previous siblings:")
for tag in tag1.find_previous_siblings():
   print (tag)

Output

<i>Python</i>
<b>Excellent</b>

Example 2

The web page (index.html) has a HTML form with three input elements. We locate one with id attribute as marks and then find its previous siblings.

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')

tag = soup.find('input', {'id':'marks'})
sibs = tag.find_previous_sibling()
print (sibs)

Output

[<input id="age" name="age" type="text"/>, <input id="nm" name="name" type="text"/>]

Example 3

The HTML string has two <p> tags. We find out the siblings previous to the one with id1 as its id attribute.

html = '''
<p><b>Excellent</b><p>Python</p><p id='id1'>Tutorial</p></p>
'''
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')
tag = soup.find('p', id='id1')
ptags = tag.find_previous_siblings()
for ptag in ptags:
   print ("Tag: {}, Text: {}".format(ptag.name, ptag.text))

Output

Tag: p, Text: Python
Tag: b, Text: Excellent
Advertisements