python - List comprehension to return highest value -


i'm scrapping web page , need know how many pages there scrap. following:

<div class="pagination">     <a href="/travel__world-desktop-wallpapers/page/2">2</a>     <a href="/travel__world-desktop-wallpapers/page/3">3</a>     <a href="/travel__world-desktop-wallpapers/page/4">4</a>     ...     <a href="/travel__world-desktop-wallpapers/page/31">31</a>     <a href="/travel__world-desktop-wallpapers/page/32">32</a>     <a href="/travel__world-desktop-wallpapers/page/33">33</a>     <a href="/travel__world-desktop-wallpapers/page/2">next »</a> </div> 

how can set list comprehension returns me highest number of pages( in case, 33)?

you don't. set generator expression instead:

max(int(link.text)      link in soup.find('div', class_='pagination').find_all('a')     if link.text.strip().isdigit()) 

demo:

>>> bs4 import beautifulsoup >>> soup = beautifulsoup('''\ ... <div class="pagination"> ...     <a href="/travel__world-desktop-wallpapers/page/2">2</a> ...     <a href="/travel__world-desktop-wallpapers/page/3">3</a> ...     <a href="/travel__world-desktop-wallpapers/page/4">4</a> ...     ... ...     <a href="/travel__world-desktop-wallpapers/page/31">31</a> ...     <a href="/travel__world-desktop-wallpapers/page/32">32</a> ...     <a href="/travel__world-desktop-wallpapers/page/33">33</a> ...     <a href="/travel__world-desktop-wallpapers/page/2">next »</a> ... </div> ... ''') >>> max(int(link.text) link in soup.find('div', class_='pagination').find_all('a') if link.text.strip().isdigit()) 33 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -