Python wget一次下载多个文件 [英] Python wget download multiple files at once

查看:313
本文介绍了Python wget一次下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找一次下载多个文件的干净的Python Wget解决方案.

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

网址将始终相同:

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)

但是我还需要下载-2.jpg,-3.jpg,-4.jpg,-5.jpg,并将NWZV1WB重命名为 NEWCODE-1.jpg ,NEWCODE-2.jpg ...

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...

我还需要下载文件夹中的所有content(22).jpg文件,并将该文件夹的本地名称重命名为 NEWCODE ,但保留文件的原始名称

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)

wget(找不到很多关于它的文章)或请求是什么才是最好的?任何帮助表示赞赏.

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

推荐答案

例如:

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")

这篇关于Python wget一次下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆