I will answer my own question: After writing up the question, I found the answer. The answer was in the docs, but I missed it, so writing it here at stackoverflow for reference.
Question: If BeautifulSoup's find_all()
cannot find a particular class
, why does it not return None
?
html = """
<div><p class="apple">apple</p></div>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
s = '<p class="banana">banana</p>'
p = soup.find_all('p', attrs={'class':'banana'})
print(type(p))
## bs4.element.ResultSet
if p is None:
print("p is None, as expected")
else:
s = soup.p.extract()
print("p is not None... but why?")
print(s)
## p is not None... but why?
## <p class="apple">apple</p>