Why do we need to findall body and head

Screen Link:
https://app.dataquest.io/m/54/web-scraping/4/using-find-all

My Code:

title = parser.find_all('title')
title_text = head[0].text

In the code above, I used the parser to find title directly, and it works, which bring my question about the sample code in this lesson:

parser = BeautifulSoup(content, 'html.parser')

# Get a list of all occurrences of the body tag in the element.
body = parser.find_all("body")

# Get the paragraph tag.
p = body[0].find_all("p")

# Get the text.
print(p[0].text)

The bit of code below seems unnecessary? Is there any particular reason that the instruction code contains this bit?

# Get a list of all occurrences of the body tag in the element.
body = parser.find_all("body")
1 Like

hey @veratsien

I think this was a like a demo code by the author to help us understand the content and instructions of the mission. Since it has been accompanied with the comments matching the content of the mission. So it’s not like a necessity to do so, but it might help students in general to understand the workflow better.

1 Like

Gotcha. I was just wondering if it’s convention or something. :smile:

1 Like