If I want to use findNext() for n times while n is a variable, how could I achieve it?

Viewed 43

By using beautifulsoup, if I want to use findNext() for n times while n is a variable, how could I achieve it?

.findNext('td').text

Should I use a for loop to achieve it? Let's say I want to find the fifth next td element, how should I change the coding?

1 Answers

yes, a loop is the way to do it.

el = soup.find("something")
for _ in range(n):
    el = el.findNext('td')
Related