Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python wget download multiple files at once

Looking for a clean Python Wget solution of downloading multiple files at once.

The url will be always the same :

https://example.com/

So far I can do this :

import wget

print('Beginning file download with wget module')
url = 'https://example.com/new_folder/1.jpg'
wget.download(url)

But i need to download also the -2.jpg, -3.jpg , -4.jpg, -5.jpg and rename the NWZV1WB to something like NEWCODE-1.jpg, NEWCODE-2.jpg...


Also I need to download all content(22).jpg files inside a folder and rename the folder localy to something like NEWCODE, but keep the original name of the files

Here the url also is always the same :

import wget

print('Beginning file download with wget module')
url = 'https://example.com/big/1.jpg' #there's 18 jpg inside
wget.download(url)

What would be best, wget (can't find to many articles about) or requests ? Any help is appreciated.

like image 449
Andie31 Avatar asked Aug 24 '18 09:08

Andie31


People also ask

How do I download multiple files from Wget?

If you want to download multiple files at once, use the -i option followed by the path to a local or external file containing a list of the URLs to be downloaded. Each URL needs to be on a separate line. If you specify - as a filename, URLs will be read from the standard input.

How do I download multiple files in Python?

Download multiple files in parallel with Python To start, create a function ( download_parallel ) to handle the parallel download. The function ( download_parallel ) will take one argument, an iterable containing URLs and associated filenames (the inputs variable we created earlier).

How do I download multiple files at once?

Hold CTRL and click on the files you want to download. Once you have selected the files you want, right click on the last file you selected and select download.

How do I use Wget in Python?

Run the wget command below and add the --directory-prefix option to specify the file path ( C:\Temp\Downloads ) to save the file you're downloading. Open File Explorer and navigate to the download location you specified (C:\Temp\Downloads) to confirm that you've successfully downloaded the file.


1 Answers

For example:

import wget
import os
import multiprocessing

def run_process(url, output_path):
    wget.download(url, out=output_path)
    # TODO: you can write your rename logic at here using os.rename


if __name__ == '__main__':
    cpus = multiprocessing.cpu_count()
    max_pool_size = 4
    pool = multiprocessing.Pool(cpus if cpus < max_pool_size else max_pool_size)
    base_dir = os.path.dirname(os.path.abspath(__file__))
    target = "NEWCODE"
    prefix_list = ["NWZV1WB", "AWU3JAD", "NW96MRD"]
    download_list = []
    name_list = list(range(1, 23))
    name_list.extend(["zoom_side", "zoom_sole", "zoom_side-thumb"])
    for prefix in prefix_list:
        path = os.path.join(base_dir, prefix)
        if not os.path.exists(path):
            os.mkdir(path)
        if not os.path.isdir(path):
            exit()
        for name in name_list:
            download_list.append(['https://img2.tennis-warehouse.com/360/{p}/{n}.jpg'.format(n=name, p=prefix), path])

    for url, path in download_list: # change here to download other files
        print('Beginning file download with wget module {n}'.format(n=url))
        pool.apply_async(run_process, args=(url, path, ))
    # add your code here to download other files
    pool.close()
    pool.join()
    print("finish")
like image 153
Henry Avatar answered Oct 31 '22 19:10

Henry