【问题标题】:How to capture cURL output to a file?如何将 cURL 输出捕获到文件中?
【发布时间】:2012-11-23 23:26:08
【问题描述】:

我有一个包含大量这种格式的 URL 的文本文档:

URL = "sitehere.com"

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

我该怎么做?

【问题讨论】:

标签: batch-file curl


【解决方案1】:

在这种情况下可以使用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

【讨论】:

    【解决方案2】:

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

    curl -K myconfig.txt >> output.txt
    

    【讨论】:

      【解决方案3】:

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

      格式

      curl "url" -o filename
      

      示例

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

      Example_2

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

      请务必添加引号。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:
              curl -K myconfig.txt -o output.txt 
              

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

              curl -K myconfig.txt >> output.txt
              

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

              注意:-K 是可选的。

              【讨论】:

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

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

              【讨论】:

                【解决方案9】:

                如果您想将 cURL 输出复制到剪贴板而不是输出到文件,您可以在 cURL 命令后使用管道 | 来使用 pbcopy

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

                【讨论】:

                • 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也许不要尝试这个!
                【解决方案10】:

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

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

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

                【讨论】:

                猜你喜欢
                • 2020-02-28
                • 2017-07-11
                • 2011-04-28
                • 2014-10-08
                • 1970-01-01
                • 1970-01-01
                • 2016-05-01
                • 2011-01-18
                • 2014-11-22
                相关资源
                最近更新 更多