BeautifulSoup: get title tag example

Syntax

head.title

title tag example

from bs4 import BeautifulSoup


# Html source
html_source = '''
<head>
    <title>BeautifulSoup: rel attribute Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta charset="utf-8" />
    <meta name="description" content="Learn how to get rel attribute" />
    <link rel="canonical" href="https://pyonlycode.com//post/beautifulsoup-rel-attribute-example/" />
</head>
'''
# Parsing
soup = BeautifulSoup(html_source, 'html.parser')

# get head element
head = soup.head

# Print title tag
print(head.title)

Output:

<title>BeautifulSoup: rel attribute Example</title>

 

# Print title value
print(head.title.string)

Output:

BeautifulSoup: rel attribute Example