1 | initial version |
To eliminate certain results from a find_all
method in BeautifulSoup, you can use a Python filter function to remove the elements that you do not want. The filter function takes a function that evaluates each element in the list and returns True
or False
depending on whether the element should be included in the final result.
Here's an example:
from bs4 import BeautifulSoup
html = """
<html>
<body>
<div class="foo">Foo</div>
<div class="bar">Bar</div>
<div class="baz">Baz</div>
</body>
</html>
"""
soup = BeautifulSoup(html, 'html.parser')
# Find all div elements
divs = soup.find_all('div')
# Define a filter function
def filter_func(tag):
# Exclude the div element with class "bar"
return tag.has_attr('class') and 'bar' not in tag['class']
# Filter the results
filtered_divs = list(filter(filter_func, divs))
# Print the results
for div in filtered_divs:
print(div)
In this example, we use the has_attr
method to check if the class
attribute exists in each div
element. If it does, we check if the string "bar" is not in the list of classes. If this condition is met, we return True
so that the element is included in the final result. Otherwise, we return False
to exclude the element. The list
function is then used to convert the filter
object to a list.
In the end, we print the filtered div
elements. The output should be:
<div class="foo">Foo</div>
<div class="baz">Baz</div>