Extract title from a webpage using Python - GeeksforGeeks

04 Apr, 2022
Prerequisite Implementing Web Scraping in Python with BeautifulSoup, Python Urllib Module, Tools for Web Scraping
In this article, we are going to write python scripts to extract the title form the webpage from the given webpage URL.
Method 1: bs4 Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install this type the below command in the terminal.
pip install bs4
requests module allows you to send HTTP/1.1 requests extremely easily. This module also does not come built-in with Python. To install this type the below command in the terminal.
pip install requests
Approach:
Import the modules
Make requests instance and pass into URL
Pass the requests into a Beautifulsoup() function
Use the ‘title’ tag to find them all tag (‘title’)
Code:
Python3
# importing the modulesimport requestsfrom bs4 import BeautifulSoup # target urlurl = 'https://www.geeksforgeeks.org/' # making requests instancereqs = requests.get(url) # using the BeautifulSoup modulesoup = BeautifulSoup(reqs.text, 'html.parser') # displaying the titleprint("Title of the website is : ")for title in soup.find_all('title'):    print(title.get_text())
Output:
Title of the website is : 
GeeksforGeeks | A computer science portal for geeks
Methods 2: In this method, we will use urllib and Beautifulsoup modules to extract the title of the website. urllib is a package that allows you to access the webpage with the program.
Installation:
pip install urllib
Approach:
Import module
Read the URL with the request.urlopen(URL).
Find the title with soup.title from the HTML document
Implementation:
Python3
# importing the modulesfrom urllib.request import urlopenfrom bs4 import BeautifulSoup # target urlurl = 'https://www.geeksforgeeks.org/' # using the BeautifulSoup modulesoup = BeautifulSoup(urlopen(url)) # displaying the titleprint("Title of the website is : ")print (soup.title.get_text())
Output:
Title of the website is : 
GeeksforGeeks | A computer science portal for geeks
Method 3: In this method, we will use the mechanize module. It is stateful programmatic web browsing in Python. Browse pages programmatically with easy HTML form filling and clicking of links.
Installation:
pip install mechanize
Approach:
Import module.
Initialize the Browser() instance.
Retrieves the webpage content Browser.open().
Display the title with Browser.title()
Implementation:
Python3
# importing the modulefrom mechanize import Browser # target urlurl = 'https://www.geeksforgeeks.org/' # creating a Browser instancebr = Browser()br.open(url) # displaying the titleprint("Title of the website is : ")print( br.title())
Output:
Title of the website is : 
GeeksforGeeks | A computer science portal for geeks
surinderdawra388
Python web-scraping-exercises
Python-requests
Web-scraping
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists