Finding HTML Elements by Multiple Tags with BeautifulSoup
When a page mixes the elements you need across different tags, a single-tag search forces repeated passes.
BeautifulSoup lets you name several tags at once and collect all of their matches together.
One search, several tag types
BeautifulSoup accepts more than one tag name in a single search. The find_all function takes a list of the tags you want, then returns all elements matching any entry in that list. This is useful when the content you need is spread across, for example, headings and emphasized text rather than one repeated tag.
Checklist before you run the search:
- Confirm the exact tag names as they appear in the markup, in lower case as written in the example.
- Put each wanted tag in the list once; duplicates add no value.
- Decide how you will handle the mixed results, since the returned elements will not be grouped by tag.
- Verify that tags you do not want are absent from the list, because they will be excluded from the results.
Worked example: headings and bold text
The worked example parses a short document containing an h1 heading, a paragraph, a span, and a b element. Passing a list containing h1 and b to find_all returns only the heading and the bold element, in the order they appear in the document. The paragraph and span are ignored because their tags were not listed. Iterating over the result and printing each element shows the two matches.
A useful way to think about the result: the list acts as an inclusion set. An element is kept if its tag is in the set, and discarded otherwise. That distinction matters when you add more tags later — every addition widens the set and can change which parts of the page you collect.
Related questions and tutorial
Related BeautifulSoup web scraping questions:
- Which is better Scrapy or BeautifulSoup?
- Can I use XPath selectors in BeautifulSoup?
- How to find all links using BeautifulSoup and Python?
- How to find elements without specific attributes in BeautifulSoup?
- How to find HTML element by class with BeautifulSoup?
- How to find HTML elements by attribute using BeautifulSoup?
- How to find sibling HTML nodes using BeautifulSoup and Python?
- How to scrape tables with BeautifulSoup?
- How to select values between two nodes in BeautifulSoup and Python?
BeautifulSoup web scraping tutorial: Learn web scraping with BeautifulSoup
Frequently asked questions
How do I collect several different tag types in one BeautifulSoup search?
Pass a list of tag names as the first argument to find_all. The call returns every matching element in document order, so you can iterate once instead of running separate searches for each tag.
What happens to elements whose tags are not in the list?
Tags not named in the list are skipped. In the worked example, the paragraph and span elements are present in the markup but do not appear in the results.