How to find HTML elements by multiple tags with BeautifulSoup?

BeautifulSoup also supports selecting  elements by multiple tags. To achieve that, we use the function find_all, and we send a list of tags we want to extract.

For example, to extract <h1> and <b> elements, we send the tags as a list like this:

from bs4 import BeautifulSoup

html_content = '''
<h1>Header</h1>
<p>Paragraph</p>
<span>Span</p>
<b>Bold</b>
'''
soup = BeautifulSoup(html_content, 'html.parser')

headers_and_bold_text = soup.find_all(["h1", "b"])
for element in headers_and_bold_text:
  print(element)
# Output:
# <h1>Header</h1>
# <b>Bold</b>

Related BeautifulSoup web scraping questions: