Last modified: Jan 10, 2023 By Alexander Williams
BeautifulSoup Get Title tag
In this BeautifulSoup topic, we'll learn how to get the title tag and the title tag content. To get the title tag, we can use:
- .title property
- find() function
BeautifulSoup Get title tag using .title property
.title property returns the title tag if it exists. Otherwise, return None.
soup.title
In the following example, we'll get the title tag from HTML:
from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module
# 👇 HTML Source
hrml_source = '''
<html lang="en">
<head>
<title>PyTutorial | Learn BeautifulSoup</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
</html>
'''
soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing
title_tag = soup.title # 👉️ Get Title Tag
print(title_tag) # 👉️ Print Title tag
Output:
<title>PyTutorial | Learn BeautifulSoup</title>
To get the content of the title tag:
print(title_tag.string) # 👉️ Print Content of Title
Output:
PyTutorial | Learn BeautifulSoup
Now let's try HTML with no title tag.
from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module
# 👇 HTML Source
hrml_source = '''
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
</html>
'''
soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing
title_tag = soup.title # 👉️ Get Title Tag
print(title_tag) # 👉️ Print Title tag
Output:
None
when you use the .string property with a None tag, you will get the following error.
print(title_tag.sring)
Output:
AttributeError: 'NoneType' object has no attribute 'sring'
To handle this error, we will use the if condition:
print(title_tag.sring if title_tag else None)
Output:
None
BeautifulSoup Get title tag using find() function
We can also get the title tag using the find() function.
Syntax:
soup.find("title")
The above code returns the title tag if it exists. Otherwise, returns None
Example:
from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module
# 👇 HTML Source
hrml_source = '''
<html lang="en">
<head>
<title>PyTutorial | Learn BeautifulSoup</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
</html>
'''
soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing
title_tag = soup.find("title") # 👉️ Find Title Tag
print(title_tag) # 👉️ Print Title tag
Output:
<title>PyTutorial | Learn BeautifulSoup</title>
Get content of the title tag:
print(title_tag.string) # 👉️ Print Content of Title
Output:
PyTutorial | Learn BeautifulSoup
You can add the string=True parameter to the find function to avoid the AttributeError: 'NoneType' object has no attribute 'sring' error.
string=True: means find the tag that has a text value.
from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module
# 👇 HTML Source
hrml_source = '''
<html lang="en">
<head>
<title>PyTutorial | Learn BeautifulSoup</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
</html>
'''
soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing
title_tag = soup.find("title", string=True) # 👉️ Find Title Tag
print(title_tag) # 👉️ Print Title tag
Output:
<title>PyTutorial | Learn BeautifulSoup</title>