首页 » 网络 » curl工具

使用-L自动跟随重定向:

$ curl -IL http://www.samba.org/samba/ftp/stable/samba-3.6.7.tar.gz
HTTP/1.1 301 Moved Permanently
Date: Fri, 07 Sep 2012 08:16:19 GMT
Server: Apache
Location: http://ftp.samba.org/pub/samba/stable/samba-3.6.7.tar.gz
Vary: Accept-Encoding
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 200 OK
Date: Fri, 07 Sep 2012 08:16:20 GMT
Server: Apache
Last-Modified: Mon, 06 Aug 2012 17:08:42 GMT
ETag: "1250019-207d5bc-4c69bedff3a80"
Accept-Ranges: bytes
Content-Length: 34067900
Content-Type: application/x-gzip
Content-Encoding: x-gzip

使用--cookie设置Cookie:

$ curl http://www.berlinix.com/session.php --cookie "PHPSESSID=ncpjjli7ojtidgtbtce75s6ne5"

-c保存Cookie文件:

$ curl http://www.zhihu.com -c /tmp/zhihu.txt > /dev/null
$ cat /tmp/zhihu.txt 
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

www.zhihu.com   FALSE   /       FALSE   0       _xsrf   8dc090f243424818b66bba601812930e

-b读取Cookie文件:

$ curl -v http://www.zhihu.com -c /tmp/zhihu.txt -b /tmp/zhihu.txt > /dev/null
> GET / HTTP/1.1
> User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: www.zhihu.com
> Accept: */*
> Cookie: _xsrf=a4488114282143aab86c218d96bf2f74

-A设置User-Agent头部字段:

$ curl -v http://www.zhihu.com -A "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebK7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7" > /dev/null 
> GET / HTTP/1.1
> User-Agent: User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7
> Host: www.zhihu.com
> Accept: */*

使用序列下载。例如我们注意到京东商品的图片有多种尺寸,他们被放在不同的文件夹下,但文件名都一样。为了一次性下载所有的图片,我们可以这样使用curl:

$ curl -o "wc_#1.jpg" http://img14.360buyimg.com/n[0-5]/11439/4970a83e-8cf7-4a3e-b4d7-f4ca721ef290.jpg
$ ls wc*.jpg
wc_0.jpg  wc_1.jpg  wc_2.jpg  wc_3.jpg  wc_4.jpg  wc_5.jpg

首先我们指定了存储文件的格式为wc_#1.jpg,其中#1将被替换为后面的[0-5]中的一个数值。

分享

0