我有一个文本文档,其中包含一堆这种格式的 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
curl -K myconfig.txt -o output.txt
写入您指定的文件中收到的第一个输出(如果存在旧的则覆盖)。
curl -K myconfig.txt >> output.txt
将您收到的所有输出附加到指定文件。
注意:-K 是可选的。
对于单个文件,您可以使用 -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
选项。
curl --remote-name-all https://example.tld/resource{1,2,3}
这样的 URL 之前。请参阅:curl.se/docs/manpage.html#--remote-name-all
有几个选项可以将 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
在这种情况下,可以使用 curl
或 wget
。所有这 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
对于那些想要在剪贴板中复制 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
xclip
可以用来代替 Linux see this question。但是,在大多数情况下,我更喜欢 curl http://example.com -o example_com.html & cat example_com.html | pbcopy
因此,如果您不小心清除了剪贴板,则无需再次卷曲。
curl http://www.textfiles.com/etext/FICTION/fielding-history-243.txt | pbcopy
也许不要尝试这个!
使用 --trace-ascii output.txt
将 curl 详细信息输出到文件 output.txt
。
-vv
显示的“描述性信息”(SSL 信息、HTTP 动词、标头...)。其他答案都没有将其写入文件。
您需要在 "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
只需确保添加引号即可。
如果要将输出存储到桌面,请使用 git bash 中的 post 命令执行以下命令。它对我有用。
curl https://localhost:8080
--request POST
--header "Content-Type: application/json"
-o "C:\Desktop\test.txt"
有点晚了,但我认为 OP 正在寻找类似的东西:
curl -K myfile.txt --trace-ascii output.txt
写入您指定的文件中收到的第一个输出(如果存在旧的则覆盖)。
curl -K myconfig.txt >> output.txt
>>
附加,它不会覆盖。如果您想覆盖,请使用单个 >
不定期副业成功案例分享
curl url > destfile.x