chevron_left
Finding elements Cookbook
Finding elements by classFinding elements by idFinding elements that are direct descendantsFinding elements that contain a specific textFinding elements by tag nameFinding elements that contain all the specified classesFinding elements that only contain specific attributes and no other attributesFinding elements using regular expressionLimiting the number of returned resultsFinding elements by attribute
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings
Limiting the number of returned results in Beautiful Soup
Programming
chevron_rightPython
chevron_rightBeautiful Soup
chevron_rightCookbooks
chevron_rightFinding elements Cookbook
schedule Jul 1, 2022
Last updated local_offer Python●Beautiful Soup
Tags tocTable of Contents
expand_more To limit the number of returned results in Beautiful Soup, set the limit
parameter of the find_all
method.
Consider the following HTML document:
my_html = """ <p>Alex</p> <p>Bob</p> <p>Cathy</p>"""
soup = BeautifulSoup(my_html)
To limit the number of returned results to 2:
for el in soup.find_all("p", limit=2): print(el)
<p>Alex</p><p>Bob</p>
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!