w3resource
Python Web Scraping Exercises

Python Web Scraping: Check whether a page contains a title or not

Python Web Scraping: Exercise-11 with Solution

Write a Python program to check whether a page contains a title or not.

Sample Solution:-

Python Code:

from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        return None
    try:
        bsObj = BeautifulSoup(html.read(), "lxml")
        title = bsObj.body.h1
    except AttributeError as e:
        return None
    return title
    
    title = getTitle(url)
    if title == None:
      return "Title could not be found"
    else:
      return title

print(getTitle("https://www.w3resource.com/"))
print(getTitle("http://www.example.com/"))

Output:

None
<h1>Example Domain</h1>
 

Flowchart:

Python Web Scraping Flowchart: Check whether a page contains a title or not

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to that retrieves an arbitary Wikipedia page of "Python" and creates a list of links on that page
Next:Write a Python program to list all language names and number of related articles in the order they appear in wikipedia.org.

What is the difficulty level of this exercise?



Python: Tips of the Day

Python: Use Enumerate() In for Loops

>>> students = ('John', 'Mary',  'Mike')
>>> for i, student in enumerate(students):
  ...     print(f'Iteration:  {i}, Student: {student}')
  ... 
Iteration: 0, Student: John
Iteration: 1, Student: Mary
Iteration: 2, Student: Mike
>>> for i, student in enumerate(students,  35001):
  ...      print(f'Student Name: {student}, Student ID #: {i}')
  ... 
Student Name: John, Student ID #: 35001
Student Name: Mary, Student ID #: 35002
Student Name: Mike, Student ID #: 35003