Find/Find_all
BeautifulSoup
offers methods for going through HTML tags. One of them is the function .find()
. It returns the first tag which matches the parameter or None
if there are no matches:
12print(soup.find("p")) print(soup.find("h9"))
We will get the same result by accessing tags directly from the BeautifulSoup
object: print(soup.p)
.
To receive the list of all occurrences of the particular tag, we can use the built-in function of the BeautifulSoup
object .find_all()
:
print(soup.find_all("p"))
It returns the list of instances of the tag object provided by BeautifulSoup
. Tag objects offer a comfortable interface to work with their contents.
One of the most important functions of BeautifulSoup
is the ability to find the specific types of tags using their attributes:
12print(soup.find_all("p", id = "id2")) print(soup.find_all(attrs = {"class":"afterbanner", "id": "id1"}))
The functions .find()
and .find_all()
are more convenient in usage as they can work in combination with attributes and regexes.
Using scraping, you are always interested in a specific part of the website, and unique attributes can help to identify them.
Swipe to start coding
In this task, you will work with the following page.
- Create the
BeautifulSoup
object using as parametershtml
and"html.parser"
. - Print the first
div
tag using the function.find()
of the objectsoup
. - Print the
p
tag where theid
equal to"id0"
using the function.find_all()
of thesoup
oblect.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 4.76
Find/Find_all
Svep för att visa menyn
BeautifulSoup
offers methods for going through HTML tags. One of them is the function .find()
. It returns the first tag which matches the parameter or None
if there are no matches:
12print(soup.find("p")) print(soup.find("h9"))
We will get the same result by accessing tags directly from the BeautifulSoup
object: print(soup.p)
.
To receive the list of all occurrences of the particular tag, we can use the built-in function of the BeautifulSoup
object .find_all()
:
print(soup.find_all("p"))
It returns the list of instances of the tag object provided by BeautifulSoup
. Tag objects offer a comfortable interface to work with their contents.
One of the most important functions of BeautifulSoup
is the ability to find the specific types of tags using their attributes:
12print(soup.find_all("p", id = "id2")) print(soup.find_all(attrs = {"class":"afterbanner", "id": "id1"}))
The functions .find()
and .find_all()
are more convenient in usage as they can work in combination with attributes and regexes.
Using scraping, you are always interested in a specific part of the website, and unique attributes can help to identify them.
Swipe to start coding
In this task, you will work with the following page.
- Create the
BeautifulSoup
object using as parametershtml
and"html.parser"
. - Print the first
div
tag using the function.find()
of the objectsoup
. - Print the
p
tag where theid
equal to"id0"
using the function.find_all()
of thesoup
oblect.
Lösning
Tack för dina kommentarer!
Awesome!
Completion rate improved to 4.76single