简体   繁体   English

从命令行下载 .zip 文件

[英]Download a .zip file from the command line

I've been somewhat blindly trying lots of variations of curl and wget to try downloading some .zip files.我一直在盲目地尝试大量的curlwget变体来尝试下载一些.zip文件。 The first thing I tried was this:我尝试的第一件事是:

curl --klo ".\bhcdata#1.zip" "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=[1-4]"

The command above is in fact provided in the documentation by the Chicago Fed , and yet it throws error messages on my Ubuntu machine: curl: option --klo: is unknown .上面的命令实际上是由 Chicago Fed 的文档提供,但它在我的 Ubuntu 机器上抛出错误消息: curl: option --klo: is unknown

Is there a straightforward way to do this?有没有直接的方法来做到这一点?

To clarify exactly what I'm trying to do: The Chicago Fed website lets you enter a year and quarter, and you click "Download data file" and it gives you a .zip file of the corresponding data.确切地说明我要做什么:芝加哥联储网站允许您输入年份和季度,然后单击“下载数据文件”,它会为您提供相应数据的 .zip 文件。 I want to do this for all quarters, so I need a way to write the command for each quarter so that I can loop over them.我想对所有季度都这样做,所以我需要一种方法来为每个季度编写命令,以便我可以遍历它们。 The [1-4] in the example command above would grab all four quarters in one year, but I'd be fine with just getting one quarter at a time, and I've tried replacing 1 as well.上面示例命令中的[1-4]将在一年内获取所有四个季度,但我一次只获取一个季度就可以了,我也尝试替换1 I've tried with and without various combinations of options, but nothing has worked yet.我已经尝试过使用和不使用各种选项组合,但还没有任何效果。

On my machine, the following command worked just fine:在我的机器上,以下命令工作得很好:

curl -o ./bhcdata1.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=1"

Note:笔记:

  1. single dash instead of double dash单破折号而不是双破折号
  2. I only specified the output file - with no quotes and with forward slash我只指定了输出文件 - 没有引号和正斜杠
  3. I simplified the URL to a specific quarter only我仅将 URL 简化为特定季度

I found a file named bhcdata1.zip in my directory when I was done:完成后我在我的目录中发现了一个名为bhcdata1.zip的文件:

-rw-r--r--   1 floris  floris    1545868 Feb 13 21:05 bhcdata1.zip

You might find you need the -k flag as well… although I didn't need to on my machine.你可能会发现你也需要-k标志......虽然我不需要在我的机器上。 Then it would be然后就是

curl -ko ./bhcdata1.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=1"

worked equally well...工作得同样好......

BONUS to get all four quarters, you can simply put the following lines in a script.奖金得到所有四个季度,你可以简单地把下面的行的脚本。 Save it as downloadAll .将其另存为downloadAll Change the file to "executable" with chmod 755 downloadAll , then type ./downloadAll .使用chmod 755 downloadAll将文件更改为“可执行”,然后键入./downloadAll Enjoy…享受…

#!/bin/bash
for i in {1..4}
do
  curl -ko ./bhcdata$i.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=$i"
done

Result:结果:

-rw-r--r--   1 floris  floris    1545868 Feb 13 21:11 bhcdata1.zip
-rw-r--r--   1 floris  floris    2413876 Feb 13 21:11 bhcdata2.zip
-rw-r--r--   1 floris  floris    1573810 Feb 13 21:11 bhcdata3.zip
-rw-r--r--   1 floris  floris    2500525 Feb 13 21:12 bhcdata4.zip

and if you wanted multiple years, do (some variant of)如果你想要多年,做(一些变体)

#!/bin/bash
for yr in {2010..2012}
do
  for qtr in {1..4}
  do
    curl -ko ./bhcdata$yr_$qtr.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=$yr&DQTR=$qtr"
  done
done

Looks like you have the wrong URL.看起来您输入了错误的网址。 Try尝试

wget "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2013&DQTR=1"

You should be able to loop the year and quarter then你应该能够循环年份和季度然后

The following bash script is my end product:以下 bash 脚本是我的最终产品:

#!/bin/bash
for i in {1986..2011}
do
  for j in {1..4}
  do
  curl -ko year${i}month${j}.zip "https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=$i&DQTR=$j"
  done
done

curl file_name_url.zip -O -J -L

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM