• curl命令的基本用法


    我们知道在linux环境下,可以调用curl下载网页。

    但curl有些高级的应用,只需要几行命令行,可能比你写多行php、python、C++的程序要快些。

    下面从问题驱动的角度来谈谈curl的用法

    1. 下载页面,保存到文件

    curl www.baidu.com

    会将网页数据输出到标准输出终端,如要保存到文件,则需要利用

    -o/--output <file>
     Write output to <file> instead of stdout.
    

    2. 批量下载多个页面

    通过{} 和[] 来标识批量下载的pattern(学过正则的同学都懂)。

    这里面同样涉及到保存文件的问题,需要#1 这样的占位符来标识,curl会负责替换之,

    具体如下

    curl http://{one,two}.site.com -o "file_#1.txt"
    
    or use several variables like:
    
    curl http://{site,host}.host[1-5].com -o "#1_#2"
    

    3. 302页面

    有时下载页面会遇到301、302的页面,这时需要继续抓取页面,curl中通过

    -L/--location
    

    注意:man curl中有这样一句话

    If this option is used twice, the second will again disable location following.

    让curl继续获取真实的页面,如果有多次跳转,可以用max-redirs 控制最大的跳转次数

    You can limit the amount of redirects to follow by using the
    
    --max-redirs option
    

    跳转涉及到url的变化,特别是有时url会在不同的domain间跳转,这时我们需要获取最终抓取页面的url

    这时需要用到 -w/--write-out <format>  参数中的${url_effective}

     url_effective  The URL that was fetched last. This is mostly  meaningful  if you've told curl to follow location: headers.
    

    4. 网络监控信息

    如果你需要得到curl下载的信息,如返回码、网络传输速度等信息,也需要用到上述提到的-w参数,其具体的参数可以参考man curl

    提几个我用到的

    stat_format="%{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total}"
    stat_format=${stat_format}":%{speed_download}:%{size_download}:%{size_request}"
    stat_format=${stat_format}":%{url_effective}"
    -w $stat_format
    

    5. 超时控制

    -m/--max-time <seconds>
                  Maximum time in seconds that you allow the whole operation to take.

    --connect-timeout <seconds>
                  Maximum time in seconds that you allow the connection to the server to take

    6. 指定UA

    -A 或者user-agent参数指定,注意需要添加"".

    常见的ua有

    'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)',
     'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)',
     'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
     'Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1',
     'Opera/9.27 (Windows NT 5.2; U; zh-cn)',
     'Opera/8.0 (Macintosh; PPC Mac OS X; U; en)',
     'Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 ',
     'Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13'
    移动端
    ’Mozilla/5.0 (iPhone 5; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit‘
  • 相关阅读:
    基于jquery和bootstrap的下拉框左右选择功能
    移动端监听输入手机号以及判断手机号有效
    Latex中文识别texlive中文
    C++、堆栈
    项目大作业图书管理系统
    DIV页面分部
    Eclipse建立Java Web项目
    Java Web开发时JSP乱码问题
    python sdnu校园网模拟登陆
    pip 安装命令
  • 原文地址:https://www.cnblogs.com/westfly/p/4115086.html
Copyright © 2020-2023  润新知