如何通过终端 (SSH) 下载 CSV?

Ton*_*yGW 2 csv curl

我正在尝试从他们的政府网站下载芝加哥犯罪统计数据(CSV 格式)。这是下载链接:

https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv?accessType=DOWNLOAD
Run Code Online (Sandbox Code Playgroud)

但它仅在您将其复制到浏览器并按 Enter 键时才有效。

我想知道如何在终端上下载 csv 文件?我可以用吗:

curl -O https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv?accessType=DOWNLOAD > Chicago.csv
Run Code Online (Sandbox Code Playgroud)

我想在 ssh 上将 Chicago.csv 保存到我当前的工作目录中。

sss*_*fff 6

您的命令有效,但是“计算”文件需要很长时间,该文件很大(5360469 行,下载 215 MB 后,我只有 881705 行,因此最终文件大小应该约为 1.3GB)。

如果您尝试使用另一组(假设“流感疫苗诊所位置 - 2012”,1058 行,192kB),您可以看到您的命令运行良好,即使它没有写入 Chicago.csv。

看看手册页:

-o, --output <file>
          Write  output to <file> instead of stdout.
-O, --remote-name
          Write output to a local file named like the remote file we get. (Only the file part of the remote file is used, the path is cut off.)
Run Code Online (Sandbox Code Playgroud)

当您使用以下命令时:

curl -O https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv?accessType=DOWNLOAD > Chicago.csv
Run Code Online (Sandbox Code Playgroud)

数据写入rows.csv?accessType=DOWNLOAD,stdout 保持为空,因此 Chicago.csv 文件将保持为空。

相反,您应该使用:

curl -o Chicago.csv https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv?accessType=DOWNLOAD
Run Code Online (Sandbox Code Playgroud)

或者:

curl https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv?accessType=DOWNLOAD > Chicago.csv
Run Code Online (Sandbox Code Playgroud)