import requests import glob import shutil from selenium import webdriver import time import os import sys import re from bs4 import BeautifulSoup from selenium.webdriver.chrome.options import Options
defdownloading_images(prefix, url): filename = url.replace(prefix, '') basename = os.path.basename(filename) dirname = filename.replace(basename, '') dirurl = url.replace(basename, '') ifnot os.path.exists(dirname): os.makedirs(dirname) response_id = True index = 1 while response_id != False: url_path = dirurl + str(index) + ".jpg" response = requests.get(url_path) response_id = response.ok filename = dirname + str(index) + ".jpg" if response_id == True: print(url_path, filename) withopen(filename,'wb') as f: for chunk in response.iter_content(128): f.write(chunk) index += 1
for dir_ in dirs: dir_path = os.path.join(dirname, str(dir_)) for file in os.listdir(dir_path): filename = os.path.join(dir_path, file) shutil.copy(filename, os.path.join(new_dirname, str(index) + ".jpg")) index += 1
dirs = sorted([int(value) for value in os.listdir(dirname)])
t_dir = dirs[0]
t_path = os.path.join(dirname, str(t_dir))
mark = False for f in os.listdir(t_path): t_t_dir = os.path.join(t_path, f) if os.path.isdir(t_t_dir): mark = True shutil.move(t_t_dir, os.path.join(dirname, f)) if mark == True: os.rmdir(t_path)
dirs = sorted([int(value) for value in os.listdir(dirname)])
for dir_ in dirs: path_dir = os.path.join(dirname, str(dir_)) # print(path_dir)
dir_ = sorted([int(value.split(".")[0]) for value in os.listdir(path_dir)]) for file in dir_: filename = os.path.join(path_dir,str(file) + ".jpg") new_filename = os.path.join(new_dirname,str(index) + ".jpg") print(filename, new_filename) shutil.copy(filename,new_filename) index += 1