1
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 循环不会停止,我测试了页面,发现它超过了 23 的 max_page,但它根本没有停止。有人可以帮忙吗?提前谢谢...

4

2 回答 2

9

max_page is a string, not a number.

>>> 1 < '0'
True
于 2013-07-02T03:32:10.373 回答
3

There are quite a few issues here:

  • You never increment page so it will never reach the value of max_page (edit: now fixed in your example)
  • Following the above in your edit, the increment will only happen if the file is open successfully
  • There is an intent error just below your with block
  • max_page isn't a number which will cause issues as Ignacio points out
  • The try: continue: block you have means that if there is an error assigning max_page, it won't be assigned again causing comparison issues

This should fix most of your issues:

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=int(get_max_page(chap_url))
        while(page<=max_page):
            page_url=chap_url+str(page)+".html"
            try:        
                pic_url=get_pic(page_url)
                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)
            except:
                break
            page+=1
    except:
        continue
    os.chdir("../")
    start_chapter+=1
于 2013-07-02T03:32:20.530 回答