【问题标题】:Download multiple URLs with curl without repeating the arguments使用 curl 下载多个 URL 而无需重复参数
【发布时间】:2021-12-25 15:05:56
【问题描述】:

我正在尝试使用 curls 下载多个 URL:

user@PC:~$ curl -LOJ "https://example.com/foo.jpg" "https://example.com/bar.jpg"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   445  100   445    0     0    517      0 --:--:-- --:--:-- --:--:--   518
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
                <title>404 - Not Found</title>
        </head>
        <body>
                <h1>404 - Not Found</h1>
                <script type="text/javascript" src="//wpc.75674.betacdn.net/0075674/www/ec_tpm_bcon.js"></script>
        </body>
</html>
user@PC:~$ ls foo.jpg
foo.jpg
user@PC:~$ ls bar.jpg
ls: cannot access 'bar.jpg': No such file or directory

但它只将参数 (-LOJ) 应用于第一个 URL,因此只下载第一个文件。

如果我为每个 URL 重复参数,则不再出现此问题,并且两个文件都已下载:

user@PC:~$ curl -LOJ "https://example.com/foo.jpg" -LOJ "https://example.com/bar.jpg"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   445  100   445    0     0    481      0 --:--:-- --:--:-- --:--:--   481
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   445  100   445    0     0   1534      0 --:--:-- --:--:-- --:--:--  1539
user@PC:~$ ls foo.jpg
foo.jpg
user@PC:~$ ls bar.jpg
bar.jpg

那么有没有办法让参数应用于传递给 curl 的所有 URL,而不必为每个 URL 重复它?

【问题讨论】:

    标签: bash curl http-status-code-404


    【解决方案1】:

    你需要使用-n1,点赞

    { echo "https://example.com/foo.jpg"; echo "https://example.com/bar.jpg"; } | xargs -n1 curl -LOJ
    

    告诉 xargs 为每个 url 启动一个 curl,而不是运行一个带有两个 url 作为参数的 curl。

    【讨论】:

      【解决方案2】:

      那么有没有办法让参数应用于传递给 curl 的所有 URL,而不必为每个 URL 重复它?

      • 如果你有 curl v7.19.0 或更高版本,有--remote-name-all,以避免重复-O-L-J 只需要输入一次。

      • 如果没有,你可以使用make-url-list | sed 's/^/-O /' | xargs curl -JL

      • 如果您有wget,它在--content-disposition--trust-server-names 中具有与-J-O 类似的选项,适用于所有给定的URL。

      【讨论】:

        猜你喜欢
        • 2011-08-22
        • 2014-06-24
        • 2013-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 2016-07-29
        相关资源
        最近更新 更多