Beautiful Soup - Functions Reference

Beautiful Soup Useful Resources

Beautiful Soup - parent Property



Description

The parent property in BeautifulSoup library returns the immediate parent element of the said PegeElement. The type of the value returned by the parents property is a Tag object. For the BeautifulSoup object, its parent is a document object

Syntax

Element.parent

Return value

The parent property returns a Tag object. For Soup object, it returns document object

Example - Usage of parent Property

This example uses .parent property to find the immediate parent element of the first <p> tag in the example HTML string.

html = """
<html>
   <head>
      <title>TutorialsPoint</title>
   </head>
   <body>
      <p>Hello World</p>
"""
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')
tag = soup.p
print (tag.parent.name)

Output

body

Example - Checking parent property of Title tag

In the following example, we see that the <title> tag is enclosed inside a <head> tag. Hence, the parent property for <title> tag returns the <head> tag.

html = """
<html>
   <head>
      <title>TutorialsPoint</title>
   </head>
   <body>
      <p>Hello World</p>
"""
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')
tag = soup.title
print (tag.parent)

Output

<head><title>TutorialsPoint</title></head>

Example - Usage of parent property with different Parsers

The behaviour of Python's built-in HTML parser is a little different from html5lib and lxml parsers. The built-in parser doesn't try to build a perfect document out of the string provided. It doesn't add additional parent tags like body or html if they don't exist in the string. On the other hand, html5lib and lxml parsers add these tags to make the document a perfect HTML document.

html = """
<p><b>Hello World</b></p>
"""
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')
print (soup.p.parent.name)

soup = BeautifulSoup(html, 'html5lib')
print (soup.p.parent.name)

Output

[document]
Body

As the HTML parser doesn't add additional tags, the parent of parsed soup is document object. However, when we use html5lib, the parent tag's name property is Body.

Advertisements