from bs4 import BeautifulSoup html_doc = """My Page Welcome to my page
This is some text.
""" soup = BeautifulSoup(html_doc, 'html.parser') # Find the first paragraph tag and get its text content p = soup.find('p') text = p.getText() print(text) # Output: This is some text.
` tag in the document, and the `getText()` method to get its text content.
2. Access attributes of an HTML tag.
We can also use BeautifulSoup to access the attributes of HTML tags. Here's an example:
from bs4 import BeautifulSoup html_doc = """In this example, we used the `find()` method to find the first `My Page Welcome to my page
This is important text.
This is not important text.
""" soup = BeautifulSoup(html_doc, 'html.parser') # Find the first paragraph tag with class="important" p = soup.find('p', {'class': 'important'}) # Access the "class" attribute of thetag class_attr = p['class'] print(class_attr) # Output: ['important']
` tag with the `class` attribute set to `"important"`. We then accessed the `class` attribute of the tag using square brackets notation. The package/library used in these examples is BeautifulSoup, which can be installed using `pip`.