如何捕获Curl输出到文件?

Ton*_*ony 367 curl batch-file

我有一个文本文档,其中包含以这种格式的一堆URL:

URL = "sitehere.com"
Run Code Online (Sandbox Code Playgroud)

我要做的是运行curl -K myfile.txt,并将响应Curl的输出返回到文件中.

我怎样才能做到这一点?

Ale*_*php 540

curl -K myconfig.txt -o output.txt 
Run Code Online (Sandbox Code Playgroud)

写入您指定的文件中收到的第一个输出(如果存在旧的输出则覆盖).

curl -K myconfig.txt >> output.txt
Run Code Online (Sandbox Code Playgroud)

将您收到的所有输出追加到指定的文件中.

  • 我使用重定向作为命令行:`curl url> destfile.x` (19认同)
  • @kris你可能在网址中有一个&符号.将网址放在双引号中,然后尝试 (3认同)
  • 当我执行其中任何一项操作时,输出仍显示在终端中,而不是文件中 (2认同)

Gre*_*ray 139

对于单个文件,您可以使用-O而不是-o filename使用URL路径的最后一段作为文件名.例:

curl http://example.com/folder/big-file.iso -O
Run Code Online (Sandbox Code Playgroud)

将结果保存到当前文件夹中名为big-file.iso的新文件.这样它的工作方式类似于wget,但允许您指定使用wget时不可用的其他curl选项.

  • 对于多个文件,请使用 `--remote-name-all` https://unix.stackexchange.com/a/265819/171025 (4认同)
  • qwr 注释的另一个补充:参数需要放在像 `curl --remote-name-all https://example.tld/resource{1,2,3}` 这样的 URL 之前。请参阅:https://curl.se/docs/manpage.html#--remote-name-all (2认同)

Gab*_*les 19

在这种情况下可以使用或curlwget所有这 3 个命令都执行相同的操作,从 http://path/to/file.txt 下载文件并将其本地保存到“my_file.txt”中。

请注意,在下面的所有命令中,我还建议使用-L--location选项,curl以便遵循 HTML 302 重定向到文件的新位置(如果文件已移动)。wget不需要额外的选项来执行此操作,因为它会自动执行此操作。

# save the file locally as my_file.txt

wget http://path/to/file.txt -O my_file.txt  # my favorite--it has a progress bar
curl -L http://path/to/file.txt -o my_file.txt
curl -L http://path/to/file.txt > my_file.txt
Run Code Online (Sandbox Code Playgroud)

或者,要将文件在本地保存为与远程相同的名称,请wget单独使用,或curl-Oor 一起使用--remote-name

# save the file locally as file.txt

wget http://path/to/file.txt
curl -LO http://path/to/file.txt
curl -L --remote-name http://path/to/file.txt
Run Code Online (Sandbox Code Playgroud)

请注意,-O上面所有命令中的 都是大写字母“O”。

该命令的好处wget是它显示了一个漂亮的进度条。

您可以通过比较 sha512 哈希值来证明上述 3 种技术中每组下载的文件完全相同。运行sha512sum my_file.txt上述每个命令并比较结果后,会发现所有 3 个文件都具有完全相同的 sha 哈希值(sha 和),这意味着这些文件逐字节完全相同。

参考

  1. 我在此处了解了该-L选项curlIs there a way to follow redirects with command line cURL?

另请参阅:wget下载文件并另存为不同文件名的命令


ece*_*ulm 14

有多种选项可将curl输出到文件

 # saves it to myfile.txt
curl http://www.example.com/data.txt -o myfile.txt

# The #1 will get substituted with the url, so the filename contains the url
curl http://www.example.com/data.txt -o "file_#1.txt" 

# saves to data.txt, the filename extracted from the URL
curl http://www.example.com/data.txt -O 

# saves to filename determined by the Content-Disposition header sent by the server.
curl http://www.example.com/data.txt -O -J 
Run Code Online (Sandbox Code Playgroud)


Md *_*han 6

对于那些想要在剪贴板中复制cURL输出而不是输出到文件的人,可以在cURL命令之后pbcopy使用管道来使用|.

示例:curl https://www.google.com/robots.txt | pbcopy.这会将给定URL中的所有内容复制到剪贴板.


小智 6

您需要在 "URL" -o "file_output" 之间添加引号,否则,curl 无法识别 URL 或文本文件名。

格式

curl "url" -o filename
Run Code Online (Sandbox Code Playgroud)

例子

curl "https://en.wikipedia.org/wiki/Quotation_mark" -o output_file.txt
Run Code Online (Sandbox Code Playgroud)

示例_2

curl "https://en.wikipedia.org/wiki/Quotation_mark" > output_file.txt  
Run Code Online (Sandbox Code Playgroud)

只需确保添加引号即可。