ChatGPT解决这个技术问题 Extra ChatGPT

如何将 cURL 输出捕获到文件中?

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

URL = "sitehere.com"

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

我怎样才能做到这一点?

curl http://{one,two}.example.com -o "file_#1.txt" curl.haxx.se/docs/manpage.html

A
Alex2php
curl -K myconfig.txt -o output.txt 

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

curl -K myconfig.txt >> output.txt

将您收到的所有输出附加到指定文件。

注意:-K 是可选的。


抱歉,也许我需要澄清一下——我的所有 URL 格式的文档都称为 myfile.txt,所以我执行 curl -K myfile.txt 并运行每个文件,但我没有将输出放入任何文件中。
我对命令行使用重定向:curl url > destfile.x
当我执行其中任何一项操作时,输出仍显示在终端中,而不是文件中
@kris,您的网址中可能有一个&符号。将网址放在双引号中,然后尝试
它可以在没有 -K 的情况下工作。有了它,我得到“没有指定 URL”。
G
Greg Bray

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

curl http://example.com/folder/big-file.iso -O

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


对于多个文件,请使用 --remote-name-all unix.stackexchange.com/a/265819/171025
要遵循重定向,请添加 -L 选项。
qwr 评论的另一个补充:参数需要放在像 curl --remote-name-all https://example.tld/resource{1,2,3} 这样的 URL 之前。请参阅:curl.se/docs/manpage.html#--remote-name-all
R
RubenLaguna

有几个选项可以将 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 

G
Gabriel Staples

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

wget http://path/to/file.txt -O my_file.txt  # my favorite--it has a progress bar
curl http://path/to/file.txt -o my_file.txt
curl http://path/to/file.txt > my_file.txt

注意第一个的 -O 是大写字母“O”。

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

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

另请参阅:wget command to download a file and save as a different filename


J
Jean-François Fabre

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

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

Linux 版本:curl https://www.google.com/robots.txt | xclip

Windows 版本:curl https://www.google.com/robots.txt | clip


pbcopy 仅在 MacOS 上可用。但是 xclip 可以用来代替 Linux see this question。但是,在大多数情况下,我更喜欢 curl http://example.com -o example_com.html & cat example_com.html | pbcopy 因此,如果您不小心清除了剪贴板,则无需再次卷曲。
如果您不确定有效负载的大小,也应谨慎使用。例如,您可能不想将其粘贴到文本编辑器中,但在 vim 中打开它没问题。 curl http://www.textfiles.com/etext/FICTION/fielding-history-243.txt | pbcopy 也许不要尝试这个!
P
Paolo

使用 --trace-ascii output.txt 将 curl 详细信息输出到文件 output.txt


谢谢,手册页提到这还输出了我想要存储的 -vv 显示的“描述性信息”(SSL 信息、HTTP 动词、标头...)。其他答案都没有将其写入文件。
A
AlexPixel

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

格式

curl "url" -o filename

例子

curl "https://en.wikipedia.org/wiki/Quotation_mark" -o output_file.txt

示例_2

curl "https://en.wikipedia.org/wiki/Quotation_mark" > output_file.txt  

只需确保添加引号即可。


P
Paolo

如果要将输出存储到桌面,请使用 git bash 中的 post 命令执行以下命令。它对我有用。

curl https://localhost:8080
    --request POST 
    --header "Content-Type: application/json" 
    -o "C:\Desktop\test.txt"

P
Paolo

有点晚了,但我认为 OP 正在寻找类似的东西:

curl -K myfile.txt --trace-ascii output.txt

C
Cudox

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

curl -K myconfig.txt >> output.txt

>> 附加,它不会覆盖。如果您想覆盖,请使用单个 >