在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