linux curl命令的重要用法:發送GET/POST請求,獲取網頁內容


curl是一個利用URL規則在命令行下工作的文件傳輸工具,可以說是一款很強大的http命令行工具。它支持文件的上傳和下載,是綜合

傳輸工具,但按傳統,習慣稱url為下載工具。

#使用curl發送GET請求:curl protocol://ip:port/url?args
curl https://proxy.mimvp.com/login?user=admin&passwd=12345678  

#使用curl發送POST請求: (推薦)
curl -d "key1=value1&key2=value2&key3=value3" protocol://ip:port/path
#示例1:curl -d 'post_data=i_love_mimvp.com' https://proxy.mimvp.com/ip.php        // 測試 post ,發送什么數據就返回什么數據,如 'i_love_mimvp.com'
#示例2:curl -d "user=admin&passwd=12345678" https://proxy.mimvp.com/login    // 測試 post ,模擬發送登錄的用戶名和密碼

#獲取頁面內容,默認會發送 GET 請求來獲取鏈接內容到標准輸出。
[root@node5 ~]# curl http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> 
......
id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>


#顯示 HTTP 頭,而不顯示文件內容,使用 -I 選項
[root@node5 ~]# 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: 277
Content-Type: text/html
Date: Thu, 22 Oct 2020 03:27:19 GMT
Etag: "575e1f72-115"
Last-Modified: Mon, 13 Jun 2016 02:50:26 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

#同時顯示 HTTP 頭和文件內容,使用 -i 選項
[root@node5 ~]# curl -i http://www.baidu.com

#將返回結果保存到文件,使用-o參數 
[root@node5 ~]# curl -o baidu.html http://www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  58084      0 --:--:-- --:--:-- --:--:-- 59525

#同時下載多個文件
[root@node5 ~]# curl -o baidu.txt http://www.baidu.com -o wanyi.txt http://www.163.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  55109      0 --:--:-- --:--:-- --:--:-- 55372
100   236  100   236    0     0   3062      0 --:--:-- --:--:-- --:--:--  3062

#鏈接重定向,使用-L跟隨鏈接重定向,邏輯為http://baidu.com -> http://www.baidu.com
[root@node5 ~]# curl -L http://baidu.com
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>

#使用 -c 保存 Cookie,當我們使用 cURL 訪問頁面的時候,默認是不會保存 Cookie 的。有些情況下我們希望保存 Cookie 以便下次訪問時使用。例如登陸了某個網站,我們希望再次訪問該網站時保持登陸的狀態,這時就可以現將登陸時的 Cookie 保存起來,下次訪問時再讀取。-c 后面跟上要保存的文件名。
[root@node5 ~]# curl -c "cookie.txt" http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> 
......
<title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div  href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

[root@node5 ~]# cat cookie.txt 
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.baidu.com	TRUE	/	FALSE	1603424061	BDORZ	27315

#指定proxy服務器以及其端口,很多時候上網需要用到代理服務器(比如是使用代理服務器上網或者因為使用curl別人網站而被別人屏蔽IP地址的時候),幸運的是curl通過使用內置option:-x來支持設置代理
[root@node5 ~]# curl -x 192.168.100.100:1080 http://www.linux.com

#模仿瀏覽器,有些網站需要使用特定的瀏覽器去訪問他們,有些還需要使用某些特定的版本。curl內置option:-A可以讓我們指定瀏覽器去訪問網站,如下服務器端就會認為是使用IE8.0去訪問的
[root@node5 ~]# curl -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)" http://www.baidu.com
<!DOCTYPE html><!--STATUS OK-->
<html>
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
	
	<link rel="dns-prefetch" href="//t11.baidu.com"/>
	den name=ch value=""><input type=hidden name=tn {ns_c({'fm':'behs','tab':target.name,'un':encodeURIComponent(un)});}});}for(var i = 0; i < u.length; i++) + new Date(new Date().getTime() + 10*60*1000).toGMTString();</script>
</body></html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM