Beautiful Soup 4
Beautiful Soup 4:
Beautiful Soup is a popular Python library used for web scraping tasks. It provides convenient methods and functions to extract data from HTML and XML documents. The “beautifulsoup4” package is the latest version of Beautiful Soup, compatible with Python 3.
To use Beautiful Soup, you need to install it first. You can install it using pip, the package installer for Python, by running the following command:
pip install beautifulsoup4
Once installed, you can import the library in your Python script like this:
from bs4 import BeautifulSoup
Beautiful Soup provides several methods for parsing and navigating through HTML or XML documents. Here’s a simple example that demonstrates how to extract information from an HTML document:
from bs4 import BeautifulSoup
# HTML content to be parsed
html_content = """
<html>
<head>
<title>Example HTML</title>
</head>
<body>
<h1>Welcome to Beautiful Soup</h1>
<p>This is a sample paragraph.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
"""
# Create a BeautifulSoup object
soup = BeautifulSoup(html_content, 'html.parser')
# Extract the title
title = soup.title.string
print("Title:", title)
# Extract the text of the first paragraph
paragraph = soup.p.string
print("Paragraph:", paragraph)
# Extract the items from the unordered list
items = soup.find_all('li')
print("Items:")
for item in items:
print(item.string)
In this example, we create a BeautifulSoup object by passing the HTML content and the parser type (‘html.parser’ in this case). We can then use various methods like .title
, .find_all()
, or .string
to extract specific elements or data from the document.
Beautiful Soup provides a rich set of features for navigating and manipulating HTML or XML data, such as searching for specific tags, accessing attributes, navigating the document tree, and more. You can refer to the official Beautiful Soup documentation for more information and examples: https://www.crummy.com/software/BeautifulSoup/bs4/doc/
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks