zoukankan      html  css  js  c++  java
  • Linux下命令行cURL的10种常见用法示例

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具。

    语法: # curl [option] [url]

    1. 获取页面内容

    当我们不加任何选项使用 curl 时,默认会发送 GET 请求来获取链接内容到标准输出。

    curl http://www.baidu.com
    

    2. 显示 HTTP 头

    如果我们只想要显示 HTTP 头,而不显示文件内容,可以使用 -I 选项:

    curl -I http://www.baidu.com
    

      输出为:

    HTTP/1.1 200 OK
    Server: bfe/1.0.8.18
    Date: Sat, 21 Apr 2018 02:31:12 GMT
    Content-Type: text/html
    Content-Length: 277
    Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
    Connection: Keep-Alive
    ETag: "575e1f60-115"
    Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
    Pragma: no-cache
    Accept-Ranges: bytes
    

      

    也可以同时显示 HTTP 头和文件内容,使用 -i 选项:

    curl -i http://www.baidu.com
    

      输出为:

    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
    Connection: Keep-Alive
    Content-Length: 2443
    Content-Type: text/html
    Date: Sat, 21 Apr 2018 02:32:31 GMT
    Etag: "588603ec-98b"
    Last-Modified: Mon, 23 Jan 2017 13:23:56 GMT
    Pragma: no-cache
    Server: bfe/1.0.8.18
    Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
    
    <!DOCTYPE html>
    <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
                    </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
    

      

    3.把链接页面的内容输出到本地文件中

    curl https://www.baidu.com > index.html

      也可以通过 curl 自带的 -o/-O 选项将内容保存到文件中。

    •   -o(小写的 o):结果会被保存到命令行中提供的文件名

    -O(大写的 O):URL 中的文件名会被用作保存输出的文件名

    curl -o index.html https://www.baidu.com
    curl -O http://www.hbygxh.org/html/2018/gyzxx_0309/857.html #结果,文件名为857.html 的文件里保存着内容

    注意:使用 -O 选项时,必须确保链接末尾包含文件名,否则 curl 无法正确保存文件。如果遇到链接中无文件名的情况,应该使

    用 -o 选项手动指定文件名,或使用重定向符号。

    4. 同时下载多个文件

    我们可以使用 -o 或 -O 选项来同时指定多个链接,按照以下格式编写命令:

    curl -O http://www.baidu.com/page/2/ -O http://www.baidu.com/page/3/

    或者:

    curl -o page1.html http://www.baidu.com/page/1/ -o page2.html http://www.baidu.com/page/2/

    5. 使用 -L 跟随链接重定向

    如果直接使用 curl 打开某些被重定向后的链接,这种情况下就无法获取我们想要的网页内容。例如:

        
    curl http://codebelief.com

    会得到如下提示:

    <html> 
    <head><title>301 Moved Permanently</title></head> 
    <body bgcolor="white"> 
    <center><h1>301 Moved Permanently</h1></center> 
    <hr><center>nginx/1.10.3</center> 
    </body> 
    </html>

    而当我们通过浏览器打开该链接时,会自动跳转到 http://www.codebelief.com。此时我们想要 curl 做的,就是像浏览器一样跟随链接的跳转,获取最终的网页内容。我们可以在命令中添加 -L 选项来跟随链接重定向:

        
    curl -L http://codebelief.com

    这样我们就能获取到经过重定向后的网页内容了。

    6. 使用 -A 自定义 User-Agent

    我们可以使用 -A 来自定义用户代理,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:

    curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" https://www.baidu.com

    下面我们会使用 -H 来实现同样的目的。

    7. 使用 -H 自定义 header

    当我们需要传递特定的 header 的时候,可以仿照以下命令来写:

    curl -H "Referer: www.example.com" -H "User-Agent: Custom-User-Agent" http://www.baidu.com

    可以看到,当我们使用 -H 来自定义 User-Agent 时,需要使用 "User-Agent: xxx" 的格式。

    我们能够直接在 header 中传递 Cookie,格式与上面的例子一样:

    curl -H "Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com

    另一种方式会在下面介绍。

    8. 使用 -c 保存 Cookie

    当我们使用 cURL 访问页面的时候,默认是不会保存 Cookie 的。有些情况下我们希望保存 Cookie 以便下次访问时使用。例如登陆了某个网站,我们希望再次访问该网站时保持登陆的状态,这时就可以现将登陆时的 Cookie 保存起来,下次访问时再读取。

     -c 后面跟上要保存的文件名。

    curl -c "cookie-example" http://www.example.com

     9. 使用 -b 读取 Cookie

    前面讲到了使用 -H 来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用 -b 来自定义 Cookie,命令如下:

    curl -b "JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com

    如果要从文件中读取 Cookie,-H 就无能为力了,此时可以使用 -b 来达到这一目的:

        
    curl -b "cookie-example" http://www.example.com

    即 -b 后面既可以是 Cookie 字符串,也可以是保存了 Cookie 的文件名。

    10. 使用 -d 发送 POST 请求

    我们以登陆网页为例来进行说明使用 cURL 发送 POST 请求的方法。假设有一个登录页面 www.example.com/login,只需要提交用户名和密码便可登录。我们可以使用 cURL 来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式:

    curl -d "userName=tom&passwd=123456" -X POST http://www.example.com/login
    

      在使用 -d 的情况下,如果省略 -X,则默认为 POST 方式:

    curl -d "userName=tom&passwd=123456" http://www.example.com/login

    强制使用 GET 方式

    curl -d "somedata" -X GET http://www.example.com/api

    或者使用 -G 选项:

    curl -d "somedata" -G http://www.example.com/api

    从文件中读取 data

    curl -d "@data.txt" http://www.example.com/login

    带 Cookie 登录

    当然,如果我们再次访问该网站,仍然会变成未登录的状态。我们可以用之前提到的方法保存 Cookie,在每次访问网站时都带上该 Cookie 以保持登录状态。

    curl -c "cookie-login" -d "userName=tom&passwd=123456" http://www.example.com/login

    再次访问该网站时,使用以下命令:

        
    curl -b "cookie-login" http://www.example.com/login
  • 相关阅读:
    finally语句
    安卓模拟器一直黑屏
    Failed to load C:SDKandroid-sdk-windowsuild-tools27.0.2libdx.jar
    eclipse不能自动生成R文件经验总结
    空间复杂度
    Japplet与Applet的区别
    二叉树有关公式
    子程序调用使用的数据结构
    IIC
    按键消抖
  • 原文地址:https://www.cnblogs.com/zxqblogrecord/p/8900219.html
Copyright © 2011-2022 走看看