BeautifulSoup: limit Example

Syntax

limit=Num

Limit example

from bs4 import BeautifulSoup

# Html source
html_source = '''
<div>
<h1>home 1</h1>
<h1>home 2</h1>
<h1>home 3</h1>
</div>
'''

# Parsing
soup = BeautifulSoup(html_source, 'html.parser')

# Find all <h1> tags with limit
h1_els = soup.find_all('h1', limit=2)

# Print
print(h1_els)

Output:

[<h1>home 1</h1>, <h1>home 2</h1>]