gpt4 book ai didi

Python wget一次下载多个文件

转载 作者:行者123 更新时间:2023-11-28 17:04:25 26 4
gpt4 key购买 nike

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

url 将始终相同:

https://example.com/

到目前为止我可以做到这一点:

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


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

这里的 url 也总是相同的:

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(找不到很多关于它的文章)或请求?任何帮助表示赞赏。

最佳答案

例如:

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一次下载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52000950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com