Skip to main content
 首页 » 编程设计

python之while循环不会停止

2025年05月04日8jillzhang

while start_chapter<=end_chapter: 
    os.makedirs("Chapter "+str(start_chapter)) 
    os.chdir("Chapter "+str(start_chapter)) 
    chap_url=link+"/c"+str(start_chapter)+"/" 
    page=1 
    try: 
        max_page=get_max_page(chap_url) 
    except: 
        continue 
    while(page<=max_page): 
        page_url=chap_url+str(page)+".html" 
        try:         
            pic_url=get_pic(page_url) 
        except: 
            break 
        picture_shit=urllib2.urlopen(pic_url).read() 
        with open(str(page)+"."+(pic_url.split(".")[len(pic_url.split("."))-1]), 'w') as f: f.write(picture_shit) 
        print str(start_chapter)+"::"+str(page) 
            page+=1 
    os.chdir("../") 
    start_chapter+=1 

内部的 while 循环不会停止,我测试了 page,看到它超过了 max_page,即 23,但它根本没有停止。任何人都可以帮忙解决这个问题吗?提前致谢...

请您参考如下方法:

max_page 是字符串,不是数字。

>>> 1 < '0' 
True