Answer the question
In order to leave comments, you need to log in
Find the total number of pages to parse. (pagination)?
Good afternoon,
Tell me, how can I find in the code using BS, the maximum number of pages? The number 20 is hidden in the li tag
. I'm trying to find it like this:soup.find('ul', class_='sc-pagination')
<div class="cl-pagination">
<ul class="sc-pagination" data-previous-text="Назад"
data-next-text="Дальше">
<li class="previous-page"><a
href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=1">
<as24-icon type="arrow">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 10.5 6.277">
<path d="M.674.742L5.25 4.933 9.823.743" stroke-width="2"
stroke="#949494" fill="none"></path>
</svg>
</as24-icon>
Назад</a></li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=1">1</a>
</li>
<li><a class="active" rel="nofollow">2</a></li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=3">3</a>
</li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=4">4</a>
</li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=5">5</a>
</li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=6">6</a>
</li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=7">7</a>
</li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=8">8</a>
</li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=9">9</a>
</li>
<li><a class="disabled" rel="nofollow">...</a></li>
<li>
<a href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=20">20</a>
</li>
<li class="next-page"><a
href="?&sort=price&desc=0&ustate=N%2CU&size=20&cy=NL&fregfrom=2019&atype=C&ac=0&page=3">Дальше
<as24-icon type="arrow">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 10.5 6.277">
<path d="M.674.742L5.25 4.933 9.823.743" stroke-width="2"
stroke="#949494" fill="none"></path>
</svg>
</as24-icon>
</a></li>
</ul>
</div>
Answer the question
In order to leave comments, you need to log in
from lxml import etree
str1 = """<div class="cl-pagination">
...
</div>"""
root = etree.fromstring(str1)
print(root.xpath('.//ul/li[last()-1]/a')[0].text)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question