X
X
xxx2017-02-13 21:01:26
Python
xxx, 2017-02-13 21:01:26

Get total_page when parsing?

467dfef2991241a1a76f67227e5f7530.jpg

def get_total_pages(html):
    soup = BeautifulSoup(html, 'lxml')
    pages = soup.find('div', class_='pagination-pages').find_all('a', class_='pagination-page')[-1].get('href')
    total_pages = pages.split('=')[1].split('&')[0]
    return int(total_pages)

In the first case, everything is clear, we can simply find the last one from the array and refer to the number in the reference.
But what if we don't have a last button ?
863c85132bbf4ee6a7af215290830fcf.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PANDEMIC, 2017-02-16
@InvictusManeo

In general, there is an option how to bypass enclose your loop in try except if you fail, just put break and that's it! and it stops parsing what is not there, this is such a simple but working option

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question