1.只得到一個url的http_code的狀態碼
curl -I -m 10 -o /dev/null -s -w %{http_code} http://127.0.0.1/a.html
查詢head
curl --head http://192.168.56.11:32769/
HTTP/1.1 200 OK Server: nginx/1.11.3 Date: Fri, 02 Sep 2016 17:44:14 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 26 Jul 2016 14:54:48 GMT Connection: keep-alive ETag: "579779b8-264" Accept-Ranges: bytes
2.curl命令參數詳解:
由於linux curl功能十分強大,所以命令參數十分多,下表只是愛E族(aiezu.com)帥選出來的部分參數,更多參數請運行“man curl”命令查看。
參數組 | 參數 | 描述 |
---|---|---|
url | url | 需要抓取的一到多個URLs; 多個下面通配符的方式: 1、http://{www,ftp,mail}.aiezu.com; 2、http://aiezu.com/images/[001-999].jpg; 3、http://aiezu.com/images/[1-999].html; 4、ftp://aiezu.com/file[a-z].txt |
請 求 頭 |
-H "name: value" --header "name: value" |
(HTTP)添加一個http header(http請求頭); |
-H "name:" --header "name:" |
(HTTP)移除一個http header(http請求頭); | |
-A "string" --user-agent "string" 【參考】 |
(HTTP)設置Http請求頭“User-Agent”,服務器通過“User-Agent”可以判斷客戶端使用的瀏覽器名稱和操作系統類型,偽造此參數能導致服務器做出錯誤判斷。 也可以使用“-H”, “--header option”設置此選項; |
|
-e <URL> --referer <URL> 【參考】 |
(HTTP)設置訪問時的來源頁面,告訴http服務從哪個頁面進入到此頁面; -e "aiezu.com"相當於“-H "Referer: www.qq.com"”; |
|
響 應 頭 |
-I --head |
(HTTP)只輸出HTTP-header,不獲取內容(HTTP/FTP/FILE)。 用於HTTP服務時,獲取頁面的http頭; (如:curl -I http://aiezu.com) 用於FTP/FILE時,將會獲取文件大小、最后修改時間; (如:curl -I file://test.txt) |
-i --include |
(HTTP)輸出HTTP頭和返回內容; | |
-D <file> --dump-header <file> |
(HTTP)轉儲http響應頭到指定文件; | |
cookie | -b name=data --cookie name=data 【參考】 |
(HTTP)發送cookie數據到HTTP服務器,數據格式為:"NAME1=VALUE1; NAME2=VALUE2"; 如果行中沒有“=”,將把參數值當作cookie文件名; 這個cookie數據可以是由服務器的http響應頭“Set-Cookie:”行發送過來的; |
-c filename --cookie-jar file name 【參考】 |
(HTTP)完成操作后將服務器返回的cookies保存到指定的文件; 指定參數值為“-”將定向到標准輸出“如控制台”; |
|
-j --junk-session-cookies |
(HTTP)告訴curl放棄所有的"session cookies"; 相當於重啟瀏覽器; |
|
代理 | -x host:port -x [protocol://[user:pwd@]host[:port] --proxy [protocol://[user:pwd@]host[:port] 【參考】 |
使用HTTP代理訪問;如果未指定端口,默認使用8080端口; protocol默認為http_proxy,其他可能的值包括: http_proxy、HTTPS_PROXY、socks4、socks4a、socks5; 如: --proxy 8.8.8.8:8080; -x "http_proxy://aiezu:123@aiezu.com:80" |
-p --proxytunnel |
將“-x”參數的代理,作為通道的方式去代理非HTTP協議,如ftp; | |
--socks4 <host[:port]> --socks4a <host[:port]> --socks5 <host[:port]> 【參考】 |
使用SOCKS4代理; 使用SOCKS4A代理; 使用SOCKS5代理; 此參數會覆蓋“-x”參數; |
|
--proxy-anyauth --proxy-basic --proxy-diges --proxy-negotiate --proxy-ntlm |
http代理認證方式,參考: --anyauth --basic --diges --negotiate --ntlm |
|
-U <user:password> --proxy-user <user:password> |
設置代理的用戶名和密碼; | |
數據 傳輸 |
-G --get 【參考】 |
如果使用了此參數,“-d/”、“--data”、“--data-binary”參數設置的數據,講附加在url上,以GET的方式請求; |
-d @file -d "string" --data "string" --data-ascii "string" --data-binary "string" --data-urlencode "string" 【參考】 |
(HTTP)使用HTTP POST方式發送“key/value對”數據,相當於瀏覽器表單屬性(method="POST",enctype="application/x-www-form-urlencoded") -d,--data:HTTP方式POST數據; --data-ascii:HTTP方式POST ascii數據; --data-binary:HTTP方式POST二進制數據; --data-urlencode:HTTP方式POST數據(進行urlencode); 如果數據以“@”開頭,后緊跟一個文件,將post文件內的內容; |
|
-F name=@file -F name=<file -F name=content --form name=content 【參考】 |
(HTTP)使用HTTP POST方式發送類似“表單字段”的多類型數據,相當於同時設置瀏覽器表單屬性(method="POST",enctype="multipart/form-data"),可以使用此參數上傳二進制文件。 如果字段內容以“@”開頭,剩下的部分應該是文件名,curl將會上傳此文件,如: curl -F "pic=@pic.jpg" http://aiezu.com; curl -F "page=@a.html;type=text/html" http://aiezu.com curl -F "page=@/tmp/a;filename=a.txt" http://aiezu.com 如果字段內容以“<”開頭,剩下的部分應該是文件名,curl將從文件中獲取作為此字段的值,如:curl -F "text=<text.txt" http://aiezu.com; |
|
--form-string <key=value> | (HTTP)類似於“--form”,但是“@”、“<”無特殊含義; | |
-T file --upload-file file |
通過“put”的方式將文件傳輸到遠程網址; 選項參數只使用字符"-",將通過stdin讀入文件內容; 如: cat test.txt|curl "http://aiezu.com/a.php" -T - curl "http://aiezu.com/a.php" -T - <test.txt 此參數也可以使用通配符: curl -T "{file1,file2}" http://aiezu.com curl -T "img[1-1000].png" http://aiezu.com |
|
斷點 續傳 |
-C <offset> --continue-at <offset> |
斷點續轉,從文件頭的指定位置開始繼續下載/上傳; offset續傳開始的位置,如果offset值為“-”,curl會自動從文件中識別起始位置開始傳輸; |
-r <range> --range <range> |
(HTTP/FTP/SFTP/FILE) 只傳輸內容的指定部分: 0-499:最前面500字節; -500:最后面500字節; 9500-:最前面9500字節; 0-0,-1:最前面和最后面的1字節; 100-199,500-599:兩個100字節; |
|
認證 |
--basic | (HTTP)告訴curl使用HTTP Basic authentication(HTTP協議時),這是默認認證方式; |
--ntlm | (HTTP)使用NTLM身份驗證方式,用於HTTP協議; 一般用於IIS使用NTLM的網站; |
|
--digest | (HTTP)使用HTTP Digest authentication加密,用於HTTP協議; 配合“-u/--user”選項,防止密碼使用明文方式發送; |
|
--negotiate | (HTTP)使用GSS-Negotiate authentication方式,用於HTTP協議; 它主要目的是為它的主要目的是為kerberos5認證提供支持支持; |
|
--anyauth | (HTTP)告訴curl自動選擇合適的身份認證方法,並選用最安全的方式; | |
-u user:password --user user:password |
使用用戶名、密碼認證,此參數會覆蓋“-n”、“--netrc”和“--netrc-optional”選項; 如果你只提供用戶名,curl將要求你輸入密碼; 如果你使用“SSPI”開啟的curl庫做“NTLM”認證,可以使用不含用戶名密碼的“-u:”選項,強制curl使用當前登錄的用戶名密碼進行認證; 此參數相當於設置http頭“Authorization:”; |
|
證書 | -E <證書[:密碼]> --cert <證書[:密碼]> |
(SSL)指定“PEM”格式的證書文件和證書密碼; |
--cert-type <type> | (SSL)告訴curl所提供證書的類型:PEM、DER、ENG等; 默認為“PEM”; |
|
--cacert <CA證書> | (SSL)告訴curl所以指定的CA證書文件,必須是“PEM”格式; | |
--capath <CA證書路徑> | (SSL)告訴curl所以指定目錄下的CA證書用來驗證; 這些證書必須是“PEM”格式; |
|
--crlfile <file> | (HTTPS/FTPS)提供一個PEM格式的文件,用於指定被吊銷的證書列表; | |
-k --insecure |
(SSL)設置此選項將允許使用無證書的不安全SSL進行連接和傳輸。 | |
SSL 其他 |
--ciphers <list of ciphers> | (SSL)指定SSL要使用的加密方式;如:“aes_256_sha_256”; |
--engine <name> | 設置一個OpenSSL加密引擎用於加密操作; 使用“curl --engine list”查看支持的加密引擎列表; |
|
--random-file | (SSL)指定包含隨機數據的文件路徑名;數據是用來為SSL連接產生隨機種子為; | |
--egd-file <file> | (SSL)為隨機種子生成器EGD(Entropy Gathering Daemon socket)指定的路徑名; | |
-1/--tlsv1 --tlsv1.0 --tlsv1.1 --tlsv1.2 -2/--sslv2 -3/--sslv3 |
(SSL)使用TLS版本2與遠程服務器通訊; (SSL)使用TLS 1.0版本與遠程服務器通訊; (SSL)使用TLS 1.1版本與遠程服務器通訊; (SSL)使用TLS 1.2版本與遠程服務器通訊; (SSL)使用SSL版本2與遠程服務器通訊; (SSL)使用SSL版本3與遠程服務器通訊; |
|
私鑰 公鑰 |
--key <key> | (SSL/SSH)指定一個私鑰文件名;為指定時自動嘗試使用下面文件:“~/.ssh/id_rsa”、“~/.ssh/id_dsa”、“./id_rsa'”、 “./id_dsa”; |
--key-type <type> | (SSL)指定私鑰文件類型,支持:DER、PEM、ENG,默認是PEM; | |
--pass <phrase> | (SSL/SSH)指定私鑰文件的密碼; | |
--pubkey <key> | (SSH)使用指定文件提供的您公鑰; | |
FTP | -P --ftp-port <接口> |
(FTP)FTP主動模式時,設置一個地址等待服務器的連接,如: 網卡:eth1 IP:8.8.8.8 主機名:aiezu.com 可以加端口號:eth1:20000-21000; |
--crlf | (FTP)上傳時將換行符(LF)轉換為回車換行(CRLF); | |
--ftp-account [data] | (FTP)ftp帳號信息; | |
--ftp-method [method] | (FTP)可選值:multicwd/nocwd/singlecwd; | |
--ftp-pasv | (FTP)使用使用PASV(被動)/EPSV模式; | |
--ftp-skip-pasv-ip | (FTP)使用PASV的時,跳過指定IP; | |
--ftp-create-dirs | (FTP)上傳時自動創建遠程目錄; | |
-l --list-only |
(FTP)列出ftp文件列表; | |
-B --use-ascii |
(FTP/LDAP)使用Ascii傳輸模式,用於FTP、LDAP;在ftp中相當與使用了“type=A;”模式。 | |
--disable-epsv | (FTP)告訴curl在PASV(被動模式)時不要使用EPSV; | |
--disable-eprt | (FTP)告訴curl在主動模式時禁用EPRT和LPRT; | |
限速 | --limit-rate <speed> | 限制curl使用的最大帶寬;如果未指定單位,默認單位為“bytes/秒”,你也可以指定單位為“K”、“M”、“G”等單位,如:“--limit-rate 1m”為限制最大使用帶寬為“1m字節/秒”; |
-y --speed-time <time> |
If a download is slower than speed-limit bytes per second during a speed-time period, the download gets aborted. If speed-time is used, the default speed-limit will be 1 unless set with -Y. This option controls transfers and thus will not affect slow connects etc. If this is a concern for you, try the --connect-timeout option. |
|
-Y --speed-limit <speed> |
If a download is slower than this given speed (in bytes per second) for speed-time seconds it gets aborted. speed-time is set with -y and is 30 if not set. | |
其他 選項 |
-0/--http1.0 | (HTTP) 強制curl使用HTTP 1.0而不是使用默認的HTTP 1.1; |
--interface <name> | 使用指定的網卡接口訪問; curl --interface eth0 http://aiezu.com curl --interface 10.0.0.101 http://aiezu.com |
|
-X <command> --request <command> |
(HTTP)指定與服務器通信使用的請求方法,如:GET、PUT、POST、DELETE等,默認GET; | |
--keepalive-time <seconds> | 設置keepalive時間 | |
--no-keepalive | 關閉keepalive功能; | |
--no-buffer | 禁用對輸出流緩沖; | |
--buffer | 啟用輸出流緩沖; | |
-L --location |
(HTTP/HTTPS)追隨http響應頭“Location:”定向到跳轉后的頁面; (在http響應碼為3XX時使用,如301跳轉、302跳轉) |
|
--location-trusted | (HTTP/HTTPS)同“--location”,但跳轉后會發送跳轉前的用戶名和密碼; | |
--compressed | (HTTP)請求對返回內容使用壓縮算法進行壓縮;curl支持對gzip壓縮進行解壓; | |
--connect-timeout <seconds> | 指定最大連接超時,單位“秒”; | |
-m seconds --max-time seconds |
限制整個curl操作的最長時間,單位為秒; | |
-s --silent |
安靜模式。不要顯示進度表或錯誤消息; | |
-# --progress-bar |
顯示進度條; | |
錯誤 選項 |
-f --fail |
(HTTP)連接失敗時(400以上錯誤)不返回默認錯誤頁面,而是返回一個curl錯誤碼“22”; |
--retry <num> --retry-delay <seconds> --retry-max-time <seconds> |
失敗重試次數; 重試間隔時間; 最大重試時間; |
|
-S --show-error |
安靜模式下顯示錯誤信息; | |
--stderr <file> | 錯誤信息保存文件; | |
輸出 | -o file --output file |
將返回內容輸出到文件。 如果是用過通配符獲取多個url,可以使用“#”后跟“數字序號”,curl會自動將它替換對應的關鍵詞,如: curl "http://aiezu.com/{a,b}.txt" -o "#1.txt"; 將保存為:“a.txt”,“b.txt”; curl "http://aiezu.com/{a,b}_[1-3].txt" -o "#1#2.txt"; 將保存為:a1.txt、a2.txt、a3.txt、b1.txt、b2.txt、b3.txt 如果要根據規則創建保存目錄,參考:“--create-dirs” 指定“-”將定向到標准輸出“如控制台”; |
-O --remote-name |
將返回內容輸出到當前目錄下,和url中文件名相同的文件中(不含目錄); | |
--create-dirs | 與“-o”參數配合使用,創建必要的本地目錄層次結構 | |
-w --write-out format |
操作完成后在返回信息尾部追加指定的內容;要追加的內容可以是一個字符串“string”、從文件中獲取“@filename”、從標准輸入中獲取“@-” 格式參數中可以用%{variable_name} 方式使用響應信息的相關變量,如:%{content_type}、%{http_code}、%{local_ip}...,更多變量參考“man curl”獲取; 格式參數可以使用“\n”、“\r”、“\t”等轉義字符; |
|
調試 | --trace <file> | 轉儲所有傳入和傳出的數據到文件,包括描述信息; 使用“-”作為文件名將輸出發送到標准輸出。 |
--trace-ascii file | 轉儲所有傳入和傳出的數據到文件,包括描述信息,只轉儲ASCII部分,更容易閱讀; 使用“-”作為文件名將輸出發送到標准輸出。 這個選項會覆蓋之前使用的-v、 --verbose、 --trace-ascii選項; |
|
--trace-time | 轉儲文件中添加時間信息; | |
-K --config <config file> |
從配置文件中讀取參數,參考:http://curl.haxx.se/docs/ | |
-v --verbose |
顯示更詳細的信息,調試時使用; | |
幫助 | -M --manual |
顯示完整的幫助手冊; |
-h --help |
linux curl用法幫助; |
curl 用法:
1、下載頁面:
1
|
curl -o index.html http:
//aiezu
.com
|
2、下載文件並顯示簡單進度條:
1
|
curl -
# -o centos6.8.iso http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso
|
3、斷點續傳:
1
2
|
#繼續完成上次終止的未完成的下載
curl -
# -o centos6.8.iso -C - http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso
|
4、偽造來源頁面:
1
2
|
#告訴愛E族,我是從百度來的
curl -e http:
//baidu
.com http:
//aiezu
.com
|
5、偽造代理設備:
1
2
3
4
5
|
#告訴愛E族,我是GOOGLE爬蟲蜘蛛(其實我是curl命令)
curl -A
" Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
http:
//aiezu
.com
#告訴愛E族,我用的是微信內置瀏覽器
curl -A
"Mozilla/5.0 AppleWebKit/600 Mobile MicroMessenger/6.0"
http:
//aiezu
.com
|
6、http頭:
1
2
|
# 看看本站的http頭是怎么樣的
curl -I http:
//aiezu
.com
|
輸出:
1
2
3
4
5
6
7
8
9
|
HTTP/1.1 200 OK
Date: Fri, 25 Nov 2016 16:45:49 GMT
Server: Apache
Set-Cookie: rox__Session=abdrt8vesprhnpc3f63p1df7j4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
|
6、設置http請求頭:
1
|
curl -H
"Cache-Control:no-cache"
http:
//aiezu
.com
|
7、發送表單數據:
1
|
curl -F
"pic=@logo.png"
-F
"site=aiezu"
http:
//aiezu
.com/
|
8、發送cookie:
1
|
curl -b
"domain=aiezu.com"
http:
//aiezu
.com
|
curl爬取過程中,會返回一個http_code,下面是他們的意義信息
$http_code["0"]="Unable to access";
$http_code["100"]="Continue";
$http_code["101"]="Switching Protocols";
$http_code["200"]=”OK”;
$http_code["201"]=”Created”;
$http_code["202"]=”Accepted”;
$http_code["203"]=”Non-Authoritative Information”;
$http_code["204"]=”No Content”;
$http_code["205"]=”Reset Content”;
$http_code["206"]=”Partial Content”;
$http_code["300"]=”Multiple Choices”;
$http_code["301"]=”Moved Permanently”;
$http_code["302"]=”Found”;
$http_code["303"]=”See Other”;
$http_code["304"]=”Not Modified”;
$http_code["305"]=”Use Proxy”;
$http_code["306"]=”(Unused)”;
$http_code["307"]=”Temporary Redirect”;
$http_code["400"]=”Bad Request”;
$http_code["401"]=”Unauthorized”;
$http_code["402"]=”Payment Required”;
$http_code["403"]=”Forbidden”;
$http_code["404"]=”Not Found”;
$http_code["405"]=”Method Not Allowed”;
$http_code["406"]=”Not Acceptable”;
$http_code["407"]=”Proxy Authentication Required”;
$http_code["408"]=”Request Timeout”;
$http_code["409"]=”Conflict”;
$http_code["410"]=”Gone”;
$http_code["411"]=”Length Required”;
$http_code["412"]=”Precondition Failed”;
$http_code["413"]=”Request Entity Too Large”;
$http_code["414"]=”Request-URI Too Long”;
$http_code["415"]=”Unsupported Media Type”;
$http_code["416"]=”Requested Range Not Satisfiable”;
$http_code["417"]=”Expectation Failed”;
$http_code["500"]=”Internal Server Error”;
$http_code["501"]=”Not Implemented”;
$http_code["502"]=”Bad Gateway”;
$http_code["503"]=”Service Unavailable”;
$http_code["504"]=”Gateway Timeout”;
$http_code["505"]=”HTTP Version Not Supported”;