Difference Between find() and find_all() in BeautifulSoup

Oct 6, 2023 ยท 2 min read

The find() and find_all() methods in the Python BeautifulSoup library are useful for searching and extracting elements from HTML and XML documents. While they may seem similar at first glance, there are some key differences in how these two methods work.

Searching vs Finding All

The main difference is that find() searches for and returns just the first matching tag or string, while find_all() returns a list containing all matching elements.

find() is useful for extracting a specific tag or text node you know exists just once in the document. find_all() is better for extracting all elements matching a criteria, like every instance of a certain tag.

soup.find('p') # Returns first <p>

soup.find_all('p') # Returns list of all <p> tags

So find_all() gives you all matches, while find() only gives the first one.

Handling No Results

If no matches are found, find() returns None, while find_all() returns an empty list.

soup.find('non-existing') # Returns None

soup.find_all('non-existing') # Returns empty list []

So check for None vs empty list depending on the method used.

Search Parameters

Both find() and find_all() take the same search criteria arguments - strings, regular expressions, lists, or functions.

# Search by string
soup.find('p')
soup.find_all('p')

# Search by regex
import re
soup.find(re.compile('b'))
soup.find_all(re.compile('b'))

The search syntax is the same for both methods.

Limiting to a Tag

To limit searching to a specific tag's children, pass that tag as the first argument to either method:

content = soup.find(id="content")

content.find('p') # Search within content tag only
content.find_all('p') # Search within content tag only

This narrowing of scope works for both find() and find_all().

Conclusion

In summary, find() gives the first match while find_all() gives all matches, and they take the same search criteria arguments. Use find() when expecting a single result, and find_all() when expecting multiple results.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: