Finding the last successful route by ID using Python
If I understand the problem correctly, the goal is still to parse all the available pages - you can initiate an endless loop and break it once the status code is not 200. And, we may reuse the same web-scraping session to improve on performance:

URL_TEMPLATE = 'http://cornpalace.com/gallery.aspx?PID={page}'

with requests.Session() as session:
    page = 1
    while True:
        response = session.get(URL_TEMPLATE.format(page=page), allow_redirects=False)
        if response.status_code != 200:
            break

         print("Processing page #{page}".format(page=page))

         soup = BeautifulSoup(response.text, "lxml")
         # parse the page

         page += 1