I want to use use soup.find_all
to find all HTML tags that have id
or name
attributes.
The following code works for the id attribute:
for tag in soup.find_all(attrs={"id": True}):
However, the following code with two attributes doesn't:
for tag in soup.find_all(attrs={"id":True, "name":True}):
Is it possible to do a Boolean search with bs4 that will find all tags that have one of two specific attributes (or both attributes) or will I have to search for each attribute separately?
soup.find_all(lambda element: 'name' in element.attrs or 'id' in element.attrs)
We use lambda
to access the element inside find_all
. And then, we use the in
operator to check if element.attrs
(it's a dictionary) has key name
or id
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.