Question #5933   Submitted by Answiki on 02/26/2022 at 06:01:26 PM UTC

How to get page title from HTML in Python ?

Answer   Submitted by Answiki on 02/26/2022 at 06:08:21 PM UTC

The best way to extract title from an HTML page stored in a string is to use BeautifulSoup:

from bs4 import BeautifulSoup

htmlStr = '<head><title>Header title</title></head><body><h1>Body title</h1></body>'
soup = BeautifulSoup(htmlStr, features="html5lib")

# Get page title (header)
titleHeader = soup.find('title')
print (titleHeader.string)

# Get page title (H1 title)
titleH1 = soup.find('h1')
print (titleH1.string)

3 events in history
Question by Answiki 02/26/2022 at 06:08:40 PM
How to get page title from HTML stored in a string in Python ?
Answer by Answiki on 02/26/2022 at 06:08:21 PM

The best way to extract title from an HTML page stored in a string is to use BeautifulSoup:

from bs4 import BeautifulSoup

htmlStr = '<head><title>Header title</title></head><body><h1>Body title</h1></body>'
soup = BeautifulSoup(htmlStr, features="html5lib")

# Get page title (header)
titleHeader = soup.find('title')
print (titleHeader.string)

# Get page title (H1 title)
titleH1 = soup.find('h1')
print (titleH1.string)

Question by Answiki 02/26/2022 at 06:01:26 PM
How to get page title from HTML in Python ?
# ID Query URL Count

Icons proudly provided by Friconix.