Because of the cloud development of wechat small program, there is no function similar to batch export, but they want to batch download the relevant files in the cloud storage.

Train of thought

A,

After trying to use NodeJS to traverse the folder download, I found that (1) there is no way to determine the storage path of the cloud server. (2) NodeJS adds images and packages them by converting them to Base64 encoding.

Second,

The cloud storage addresses of all files to be downloaded are stored in the database, converted into HTTP URL addresses in the front end, and sorted into XLSX files. Download it, and then use PY to download it in bulk, and sort it.

1. Firstly, export the URL address of the file to be downloaded from the database into an Excel file using JS.

2. Read XLSX, using XLRD package in py. Just read the first column of the file. Col1_values is a list object.

def main(): Sheet1 = book.sheets()[0] col1_values = sheet1.col_values(0) download(col1_values)Copy the code

3. Pass the list object col1_values into the Download function for download. Split was used to split the URL and create folders to achieve classification according to the result of segmentation. Be sure to add ‘\’

Def download(list): if pic_url = list[1:] : print(" start download: "+ key + "\r\n") try: pic = requests.get(key, timeout=5) except requests.exceptions.ConnectionError: Print (' download failed ') continue # save image dir = key.split('/')[3] # create folder dir(folder + '/' + dir Key.split ('/')[4] # folder dir = Folder + '/' + dir + '/' + dir1 'wb') fp.write(pic.content) fp.close()Copy the code
  1. Creating a folder

    Gets the path to the py file where the file is saved

    folder = os.getcwd() print(folder)

    Creating a folder

    def mkdir(path): folder = os.path.exists(path)

    Makedirs (path) # Makedirs creates a folder if the path does not existCopy the code

The final result

PS: If you need Python learning materials, please click on the link below to obtain them

Free Python learning materials and group communication solutions click to join