Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set page range stop value when it can be anything in Python

I’ve set up my first script to scrape webpages, but currently by defining a start and stop point in my range like so:

for page in range(1, 4):

However, I have no idea how many pages there will be at any given moment (value will regularly change) – what should I use as my stop value in this scenario? I want to avoid the hack of putting a ridiculously high value.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are many ways to skin this cat, but I would use the count method in itertools:

import itertools
for page in itertools.count(1):
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading