Work with the Library
Regular expressions are great for matches but a bit inconvenient. Python provides us with an instrumental library for web scrapping - BeautifulSoup
!
BeautifulSoup
makes it easy to go through HTML files and extract the parts we are interested in. To import the library, use:
from bs4 import BeautifulSoup
To create the first object and start iterating with the website, use the following code:
soup = BeautifulSoup(html, "html.parser")
We assign the Beautiful Object to the variable soup
with two parameters. The first one is the HTML file we want to parse. The second argument tells which parser to use. "html.parser"
corresponds to Python's built-in HTML parser.
BeautifulSoup
is highly comfortable to work with since you don't need to write regexes or additional conditions to extract the data from tags.
For instance, let's get the first tag of the type title
from the website:
1print(soup.title)
BeautifulSoup
can also help to convert websites into DataFrames
(using pandas
), which are easier to manipulate. We will learn how to do this in the following chapters.
Swipe to start coding
Print the first h1
of the page tag using BeautifulSoup
:
- Import the needed library.
- Create the BeautifulSoup object and assign it to the variable
soup
. - Print the first
h1
tag using the variablesoup
.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.76Awesome!
Completion rate improved to 4.76
Work with the Library
Regular expressions are great for matches but a bit inconvenient. Python provides us with an instrumental library for web scrapping - BeautifulSoup
!
BeautifulSoup
makes it easy to go through HTML files and extract the parts we are interested in. To import the library, use:
from bs4 import BeautifulSoup
To create the first object and start iterating with the website, use the following code:
soup = BeautifulSoup(html, "html.parser")
We assign the Beautiful Object to the variable soup
with two parameters. The first one is the HTML file we want to parse. The second argument tells which parser to use. "html.parser"
corresponds to Python's built-in HTML parser.
BeautifulSoup
is highly comfortable to work with since you don't need to write regexes or additional conditions to extract the data from tags.
For instance, let's get the first tag of the type title
from the website:
1print(soup.title)
BeautifulSoup
can also help to convert websites into DataFrames
(using pandas
), which are easier to manipulate. We will learn how to do this in the following chapters.
Swipe to start coding
Print the first h1
of the page tag using BeautifulSoup
:
- Import the needed library.
- Create the BeautifulSoup object and assign it to the variable
soup
. - Print the first
h1
tag using the variablesoup
.
Solution
Thanks for your feedback!
single
Awesome!
Completion rate improved to 4.76
Work with the Library
Swipe to show menu
Regular expressions are great for matches but a bit inconvenient. Python provides us with an instrumental library for web scrapping - BeautifulSoup
!
BeautifulSoup
makes it easy to go through HTML files and extract the parts we are interested in. To import the library, use:
from bs4 import BeautifulSoup
To create the first object and start iterating with the website, use the following code:
soup = BeautifulSoup(html, "html.parser")
We assign the Beautiful Object to the variable soup
with two parameters. The first one is the HTML file we want to parse. The second argument tells which parser to use. "html.parser"
corresponds to Python's built-in HTML parser.
BeautifulSoup
is highly comfortable to work with since you don't need to write regexes or additional conditions to extract the data from tags.
For instance, let's get the first tag of the type title
from the website:
1print(soup.title)
BeautifulSoup
can also help to convert websites into DataFrames
(using pandas
), which are easier to manipulate. We will learn how to do this in the following chapters.
Swipe to start coding
Print the first h1
of the page tag using BeautifulSoup
:
- Import the needed library.
- Create the BeautifulSoup object and assign it to the variable
soup
. - Print the first
h1
tag using the variablesoup
.
Solution
Thanks for your feedback!