How to limit the number of elements found by BeautifulSoup?

Viewed 639

While scraping a webpage using BeautifulSoup, is there a way to limit the number of elements found by the find method family.

For eg if I want only the first 5 tags can I do this using BeautifulSoup?

1 Answers

.find_all() and .select() return standard python list, so you can use for example [:5] to get only first 5 results:

from bs4 import BeautifulSoup

txt = '''
<div>Tag 1</div>
<div>Tag 2</div>
<div>Tag 3</div>
<div>Tag 4</div>
<div>Tag 5</div>
<div>Tag 6</div>
<div>Tag 7</div>
'''

soup = BeautifulSoup(txt, 'html.parser')

for div in soup.find_all('div')[:5]:
    print(div.text)

Prints:

Tag 1
Tag 2
Tag 3
Tag 4
Tag 5

EDIT: You can use CSS selector for selecting first 5 elements:

from bs4 import BeautifulSoup

txt = '''
<div>Tag 1</div>
<div>Tag 2</div>
<div>Tag 3</div>
<div>Tag 4</div>
<div>Tag 5</div>
<div>Tag 6</div>
<div>Tag 7</div>
'''

soup = BeautifulSoup(txt, 'html.parser')

for div in soup.select('div:nth-of-type(-n+5)'):
    print(div.text)

Prints:

Tag 1
Tag 2
Tag 3
Tag 4
Tag 5
Related