Beautifulsoup – Kinds of objects
In this article, we will discuss different types of objects in Beautifullsoup. When the string or HTML document is given in the constructor of BeautifulSoup, this constructor converts this document to different python objects.
The four major and important objects are :
1. BeautifulSoup Object: The BeautifulSoup object represents the parsed document as a whole. So, it is the complete document which we are trying to scrape. For most purposes, you can treat it as a Tag object.
2. Tag Object: Tag object corresponds to an XML or HTML tag in the original document. Further, this object is usually used to extract a tag from the whole HTML document. Further, Beautiful Soup is not an HTTP client which means to scrap online websites you first have to download them using the requests module and then serve them to Beautiful Soup for scraping. Additionally, this object returns the first found tag if your document has multiple tags with the same name.
The tag contains many methods and attributes. And two important features of a tag are its name and attributes.
# Name :
The name of the tag can be accessed through ‘.name’ as suffix.
Return: the type of tag it is.
We can also change the name of the tag.
b <Strong>Geeks for Geeks</Strong>
# Attributes :
Example 1: Anything that is NOT tag, is basically an attribute and must contain a value. A tag object can have many attributes and can be accessed either through accessing the keys or directly accessing through value. We can also modify the attributes and their value.
['gfg'] <b class="geeks">Geeks for Geeks</b> <b>Geeks for Geeks</b>
Example 2: A document may contain multi-valued attributes and can be accessed using key-value pair.
3. NavigableString Object: A string corresponds to a bit of text within a tag. Beautiful Soup uses the NavigableString class to contain these bits of text.
Syntax: <tag> String here </tag>
4. Comment Object: The Comment object is just a special type of NavigableString and is used to make the codebase more readable.