擅长:python、mysql、java
<p>您可以尝试使用异常</p>
<pre><code>while len(nextpage) > 0:
try:
for price in olxprices:
output.append(price.get_text().strip())
print(price.get_text().strip())
newUrl = nextpage[counter]['href']
olxprices = Olx(newUrl).getPrice()
counter += 1
except IndexError:
break
</code></pre>
<p>(或者做任何你想做的事情作为例外)
如果这不能回答您的问题,可能是因为页面的长度保持不变,所以您可能也希望遍历它</p>