defdownload_img_list(img_list, fold_name): width = len(str(len(img_list))) ifnot os.path.exists(f'./{fold_name}'): os.makedirs(f'./{fold_name}') for index, img in enumerate(img_list): ifnot os.path.exists(f'.//{fold_name}/{f"{index}".zfill(width)}.jpg'): img_url = requests.get(f'https:{img["data-src"]}', stream=True) with open(f'.//{fold_name}/{f"{index}".zfill(width)}.jpg', 'wb') as f: f.write(img_url.content) print(f'{index+1}/{len(img_list)} file(s) downloaded.') else: print(f'The {index+1}/{len(img_list)} had been downloaded before.') return
if __name__ == '__main__': input = sys.argv[1] if input[:2] == 'cv': temp = get_img_list_cv(f'https://www.bilibili.com/read/{input}') elif input[:2] == 'rl': temp = get_img_list(f'https://www.bilibili.com/read/readlist/{input}') for i in range(3): try: download_img_list(temp[0], temp[1]) break except Exception as e: if i == 2: print(f'Sorry but it failed. Error: {e}') break print('Something is wrong, restarting...') time.sleep(3) pass print('Done.')