Meaning of [0] in find_all

Screen Link:

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.

What is the use of [0]?
it cant be for indexing since body only contains the p branch.
how come we dont use it in parser.find_all but we need to use it for body.find_all and even p.text?

also is ResultSet the same thing as a list?

body acts as a list (technically a bs4.element.ResultSet), so body[0] returns the 1st element from body.

Technically, there can be many values in the bs4.element.ResultSet, and then, this indexer would be really useful.

For example, you could parse a list of tags:

body = parser.find_all(["body", "head"])

and then, body will have 2 values.

thank you.

does this mean I always have to use body[0] even if there is only 1 element in body?
when i run p = body.find_all("p")

i get the error: AttributeError: 'ResultSet' object has no attribute 'find_all'

Yep. This is because you want to use find_all("p") on an element of body, not on the body itself.

This is somewhat simillar to list and element of list, pandas.DataFrame versus pandas.Series (a single column in pandas.DataFrame can be treated as a pandas.Series). They have different methods (functions) you can use on them.