def dbp_loop(): try: while True: url = raw_input(u2c('\n输入豆瓣相册地址: ')) dbpurl = DbpUrl(url) if dbpurl.is_valid(): parse_html(dbpurl) else: u2c_print('输入地址不合法TT') except (KeyboardInterrupt, SystemExit): # user wants to quit res = raw_input(u2c('\n确认退出程序?(Y/N)')) if res not in 'yY': dbp_loop() except Exception: u2c_print('\n出错误了TT!') dbp_loop()
def parse_html(dbpurl): global counter # fetch html src = fetch_html(dbpurl.get_url()) html = Html(src, dbpurl) out_dir = html.get_title() # enter album cn_dirpath = make_dirs_cn(out_dir) os.chdir(cn_dirpath) # create album print(u2c('\n创建相册 ') + cn_dirpath) counter = 0 # download photoes = html.get_photoes() dl_photos(get_photo_list(photoes)) # continue download next = html.next_url() def parse_next(next): dbpurl = DbpUrl(next) # fetch html src = fetch_html(dbpurl.get_url()) html = Html(src, dbpurl) # download photoes photoes = html.get_photoes() dl_photos(get_photo_list(photoes)) next = html.next_url() if next: parse_next(next) if next: parse_next(next) # leave album os.chdir('..') u2c_print('下载总数 %d张' % counter)
def u2c_print(s): print(u2c(s))