【问题标题】:is there a way to download multiple files using the requests module有没有办法使用请求模块下载多个文件
【发布时间】:2020-06-30 18:01:54
【问题描述】:

我想从名为 hdrihaven.com 的网站下载多个 .hdr 文件。 我对 python 的了解不是很好,但这是我迄今为止尝试过的:

import requests

url = 'https://hdrihaven.com/files/hdris/'
resolution = '4k'
file = 'pump_station' #would need to be every file

url_2k = url + file + '_' + resolution + '.hdr'
print(url_2k)

r = requests.get(url_2k, allow_redirects=True)
open(file + resolution + '.hdr', 'wb').write(r.content)

理想情况下,file 会遍历目录中的每个文件。

提前感谢您的回答!

编辑

我在 github 上找到了一个可以满足我需求的脚本:https://github.com/Alzy/hdrihaven_dl。我在这里对其进行了编辑以满足我的需求:https://github.com/ktkk/hdrihaven-downloader。它使用 cmets 中建议的循环遍历所有可用文件列表的技术。

我发现请求模块以及 urllib 与从例如本地下载相比非常慢。铬合金。如果有人知道如何加快这些速度,请告诉我。

【问题讨论】:

  • 通过解析返回的内容,从相应的url获取文件列表,然后遍历该列表并单独检索每个文件。
  • 那么当你感觉很疯狂的时候,添加多处理来并行处理

标签: python python-requests


【解决方案1】:

有两种方法可以做到这一点:

  1. 您可以使用URL 来获取所有文件并通过循环迭代以单独下载它们。这当然只有在存在这样的URL 时才有效。

  2. 您可以将单个 URL 传递给可以并行/批量下载它们的函数。

例如:

import os
import requests
from time import time
from multiprocessing.pool import ThreadPool

def url_response(url):
    path, url = url
    r = requests.get(url, stream = True)
    with open(path, 'wb') as f:
        for ch in r:
            f.write(ch)

urls = [("Event1", "https://www.python.org/events/python-events/805/"),("Event2", "https://www.python.org/events/python-events/801/"),
("Event3", "https://www.python.org/events/python-user-group/816/")]


start = time()

for x in urls:
    url_response (x)
print(f"Time to download: {time() - start}")

此代码 sn-p 取自这里Download multiple files (Parallel/bulk download)。请继续阅读以了解有关如何执行此操作的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-09
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2021-06-13
    相关资源
    最近更新 更多