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‘

时间: 2024-08-05 19:17:51

curl命令的基本用法的相关文章

curl命令用法

例: char buf[512] = { 0 };sprintf(buf,"curl %s/NewWanbu/App/Api/index.php/Report/sEmail/aid/%d/reportType/final/",g_profile.m_confData.m_url.c_str(),activeID); //curl http://wanbu.lvzy/NewWanbu/App/Api/index.php/Report/sEmail/aid/3429/reportType/

shell神器curl命令的用法 curl用法实例笔记

shell神器curl命令的用法举例,如下: ##基本用法(配合sed/awk/grep) $curl http://www.jquerycn.cn ##下载保存 $curl http://www.jquerycn.cn > index.html $curl -o index.html http://www.jquerycn.cn $curl -O http://www.jquerycn.cn/target.tar.gz ##通过代理 $curl -x 123.45.67.89:1080 -o

curl命令的用法

curl 命令详解 命令事例 发送POST请求: 如果传输文件:curl -F "[email protected];type=text/plain" localhost:8080/request_body 普通post请求:curl -d "method=searchone&module=seller&user_name=wb-liqiu&nickname=dd" -H"Host:fmp.view.lz.taobao.com&qu

Linux Shell学习--curl命令详解

curl命令详解 (1).curl介绍 作为一款强力工具,curl支持包括HTTP.HTTPS.FTP在内的众多协议.它还支持POST.cookie.认证.从指定偏移处下载部分文件.参照页(referer).用户代理字符串.扩展头部.限速.文件大小限制.进度条等特性.如果要和网页访问序列(web page usagesequence)以及数据检索自动化打交道,那么curl定能助你一臂之力. (2).curl的help curl --help Usage: curl [options...] <u

Curl命令简单使用

curl命令 curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等协议.curl支持HTTPS认证,并且支持HTTP的POST.PUT等方法, FTP上传, kerberos认证,HTTP上传,代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,上载文件断点续传,,http代理服务器管道( proxy tunneling), 甚至它还支持IPv6,sock

CURL --- 命令行浏览器

来源:http://blog.163.com/sam_chen_cn/blog/static/121643873201162992241114/ CURL --- 命令行浏览器 这东西现在已经是苹果机上内置的命令行工具之一了,可见其魅力之一斑 1)二话不说,先从这里开始吧! curl http://www.yahoo.com 回车之后,www.yahoo.com 的html就稀里哗啦地显示在屏幕上了~~~~~ 2)嗯,要想把读过来页面存下来,是不是要这样呢?curl http://www.yah

Linux系统中的curl命令使用教程

这篇文章主要介绍了Linux系统中的curl命令使用教程,是Linux入门学习中的基础知识,需要的朋友可以参考下. 命令:curl在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具.语法:# curl [option] [url]常见参数:-A/--user-agent <string>              设置用户代理发送给服务器-b/--cookie

linux中shell curl命令获取http状态码--------强大的网络传输工具

文章来源:http://dbajun.iteye.com/blog/1813801 curl命令详解:http://blog.sina.com.cn/s/blog_94cf845f0102w8vt.html curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类似的工具还有wget. 常用参数 curl命令参数很多,这里只列出我曾经用过.特别是在shell脚本中

curl命令使用大全

本文是 http://www.51osos.com/a/Linux_CentOS_RedHat/Linuxjichu/2010/1025/curl.html 以及 http://hi.baidu.com/yschen0925/blog/item/d62851072f10eaca7b894790.html 的汇总. 今天51开源给大家讲细讲解curl这个命令. 可以看作命令行浏览器 1.开启gzip请求 curl -I http://www.sina.com.cn/ -H Accept-Encod