A simple example to grab the title of a page using Python and beautifulsoup4
First Published: .
A really simple code I added to my Python 3 Exercises for Beginners book, to grab the title of a Web page.
from urllib import request from bs4 import BeautifulSoup s_url = "https://blog.carlesmateo.com/movies-i-saw/" s_html = request.urlopen(s_url).read().decode('utf8') o_soup = BeautifulSoup(s_html, 'html.parser') o_title = o_soup.find('title') print(o_title.string) # Prints the tag string content # Another possible way if o_soup.title is not None: s_title = o_soup.title.string else: s_title = o_title.title print(s_title)
I also included this code in the code repository for Python 3 Combat Guide book.
https://gitlab.com/carles.mateo/python_combat_guide/-/blob/master/src/html_parse_beautifulsoup4.py
Rules for writing a Comment