从命令行下载.zip文件 [英] Download a .zip file from the command line
问题描述
我一直在盲目尝试许多 curl
和 wget
的变体,以尝试下载一些 .zip
文件。我尝试过的第一件事是这样的:
curl --klo .\bhcdata#1.zip https:/ /www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=[1-4]
实际上,以上命令在芝加哥的文档中中提供美联储,但它在我的Ubuntu计算机上抛出错误消息: curl:option --klo:is c。
有一种简单的方法可以做到这一点吗?
要明确说明我要做什么:芝加哥联储网站可让您输入年份和季度,然后单击下载数据文件并为您提供相应数据的.zip文件。我想对所有季度执行此操作,因此我需要一种为每个季度编写命令的方法,以便可以遍历它们。上面的示例命令中的 [1-4]
会在一年中占据所有四个季度,但是一次只获得一个季度就可以了,也尝试替换 1
。我尝试了各种选项的组合,也没有进行组合,但是什么都没有。
在我的机器上,以下命令工作得很好:
curl -o ./bhcdata1.zip https://www.chicagofed.org/applications/bhc_data /bhcdata_create_output.cfm?DYR=2011&DQTR=1
注意:
- 单破折号而不是双破折号
- 我只指定了输出文件-不带引号并且带有正斜杠
- 我仅将URL简化为特定季度
我找到了一个名为 bhcdata1.zip
:
-rw-r--r -1 floris floris 1545868 2月13日21:05 bhcdata1.zip
您可能会发现需要 -k
标志也是如此……尽管我不需要在我的机器上。
curl -ko ./bhcdata1.zip https://www.chicagofed.org/applications/bhc_data /bhcdata_create_output.cfm?DYR=2011&DQTR=1
运作良好...
奖金要获得全部四个季度,您只需在脚本中放入以下几行即可。将其另存为 downloadAll
。使用 chmod 755 downloadAll
将文件更改为可执行,然后键入 ./ downloadAll
。享受……
#!/ bin / bash
for {1..4}
中的i
curl -ko ./bhcdata$i.zip https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=2011&DQTR=$i
完成
结果:
-rw-r--r-- 1 floris floris 1545868 2月13日21:11 bhcdata1.zip
-rw-r--r--1 floris floris 2413876 2月13日21:11 bhcdata2.zip
-rw-r--r-- 1弗洛里斯弗洛里斯1573810 2月13日21:11 bhcdata3.zip
-rw-r--r--r-- 1弗洛里斯弗洛里斯2500525 2月13日21:12 bhcdata4.zip
如果您想要多年,可以做(的某些变体)
#!/ bin / bash
在{2010..2012}中的年度
对{1..4中的qtr做
}
做
curl -ko ./bhcdata$yr_$qtr.zip https://www.chicagofed.org/applications/bhc_data/bhcdata_create_output.cfm?DYR=$yr&DQTR=$qtr
完成
完成
I've been somewhat blindly trying lots of variations of curl
and wget
to try downloading some .zip
files. 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
.
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. 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. 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:
- single dash instead of double dash
- I only specified the output file - with no quotes and with forward slash
- I simplified the URL to a specific quarter only
I found a file named bhcdata1.zip
in my directory when I was done:
-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. 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
. Change the file to "executable" with chmod 755 downloadAll
, then type ./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
这篇关于从命令行下载.zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!