抓取百度:
1 | curl http://www.baidu.com |
如發現亂碼,可以使用iconv轉碼:
1 | curl http://iframe.ip138.com/ic.asp|iconv -fgb2312 |
iconv的用法請參閱:在Linux/Unix系統下用iconv命令處理文本文件中文亂碼問題
linux curl使用http代理抓取頁面:
1 2 | curl -x 111.95.243.36:80 http://iframe.ip138.com/ic.asp|iconv -fgb2312 curl -x 111.95.243.36:80 -U aiezu:passWord http://www.baidu.com |
使用socks代理抓取頁面:
1 2 | curl --socks4 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312 curl --socks5 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312 |
代理服務器地址可以從爬蟲代理上獲取。
接收cookies:
1 | curl -c /tmp/cookies http://www.baidu.com #cookies保存到/tmp/cookies文件 |
發送cookies:
1 2 | curl -b "key1=val1;key2=val2;" http://www.baidu.com #發送cookies文本 curl -b /tmp/cookies http://www.baidu.com #從文件中讀取cookies |
linux curl get方式提交數據:
1 | curl -G -d "name=value&name2=value2" http://www.baidu.com |
linux curl post方式提交數據:
1 2 | curl -d "name=value&name2=value2" http://www.baidu.com #post數據 curl -d a=b&c=d&txt@/tmp/txt http://www.baidu.com #post文件 |
以表單的方式上傳文件:
1 | curl -F file=@/tmp/me.txt http://www.aiezu.com |
相當于設置form表單的method="POST"和enctype='multipart/form-data'兩個屬性。
設置http請求頭信息:
1 2 3 | curl -A "Mozilla/5.0 Firefox/21.0" http://www.baidu.com #設置http請求頭User-Agent curl -e "http://pachong.org/" http://www.baidu.com #設置http請求頭Referer curl -H "Connection:keep-alive /n User-Agent: Mozilla/5.0" http://www.aiezu.com |
設置http響應頭處理:
1 2 | curl -I http://www.aiezu.com #僅僅返回header curl -D /tmp/header http://www.aiezu.com #將http header保存到/tmp/header文件 |
6. linux curl認證:
1 2 | curl -u aiezu:password http://www.aiezu.com #用戶名密碼認證 curl -E mycert.pem https://www.baidu.com #采用證書認證 |
1 2 | curl -# http://www.baidu.com #以“#”號輸出進度條 curl -o /tmp/aiezu http://www.baidu.com #保存http響應到/tmp/aiezu |
一、查看網頁源碼
直接在curl命令后加上網址,就可以看到網頁源碼。我們以網址www.sina.com為例(選擇該網址,主要因為它的網頁代碼較短)
curl www.baidu.com如果要把這個網頁保存下來,可以使用-o參數,這就相當于使用wget命令了。
curl -o [文件名] www.baidu.com二、自動跳轉 有的網址是自動跳轉的。使用-L參數,curl就會跳轉到新的網址。
curl -L http://item.taobao.com/item.htm?id=25823396605 鍵入上面的命令,結果就自動跳轉為http://detail.tmall.com/item.htm?id=25823396605
三、顯示頭信息 -i 參數可以顯示http response的頭信息,連同網頁代碼一起。
(-I 參數則是只顯示http response的頭信息。)
curl -i www.baidu.com
HTTP/1.1 200 OKDate: Fri, 28 Feb 2014 05:39:57 GMTContent-Type: text/htmlTransfer-Encoding: chunkedConnection: Keep-AliveVary: Accept-EncodingSet-Cookie: BAIDUID=0F251A658E427EBB7CBEB0C3F4A70FAE:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.comSet-Cookie: BDSVRTM=0; path=/Set-Cookie: H_PS_PSSID=4104_5231_1445_5139_5225_5378_5368_4261_4760_5400; path=/; domain=.baidu.comP3P: CP=" OTI DSP COR IVA OUR IND COM "Expires: Fri, 28 Feb 2014 05:39:45 GMTCache-Control: PRivateServer: BWS/1.1BDPAGETYPE: 1BDQID: 0xc3b306dca955703dBDUSERID: 0<!DOCTYPE html>.....
四、顯示通信過程 -v 參數可以顯示一次http通信的整個過程,包括端口連接和http request頭信息。
命令:curl -v www.baidu.com
* About to connect() to www.baidu.com port 80
* Trying 115.239.210.26... connected
* Connected to www.baidu.com (115.239.210.26) port 80
> GET / HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 28 Feb 2014 05:42:37 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: Keep-Alive
< Vary: Accept-Encoding
< Set-Cookie: BAIDUID=442AD49501EF253AE71F2BAF3E0181FB:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BDSVRTM=0; path=/
< Set-Cookie: H_PS_PSSID=5228_1461_5187_5138_5225_5379_5368_4261_4760_5401_5286; path=/; domain=.baidu.com
< P3P: CP=" OTI DSP COR IVA OUR IND COM "
< Expires: Fri, 28 Feb 2014 05:41:43 GMT
< Cache-Control: private
< Server: BWS/1.1
< BDPAGETYPE: 1
< BDQID: 0x906950d16fb1e95d
< BDUSERID: 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Spee<!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"><link rel="dns-
如果你覺得上面的信息還不夠,那么下面的命令可以查看更詳細的通信過程:
curl --trace output.txt www.baidu.com 或者
curl --trace-ascii output.txt www.baidu.com 運行后,請打開output.txt文件查看。
當然也有更狠的,查看頁面跳轉過程:
curl -L -I --trace log http://item.taobao.com/item.htm?id=25823396605
HTTP/1.1 301 Moved Permanently
Server: Tengine
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html;charset=GBK
Content-Length: 0
Connection: close
P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'
Content-Language: zh-CN
Accept-Ranges: bytes
X-Varnish: 1206961665
Via: 1.1 varnish
location: http://detail.tmall.com/item.htm?id=25823396605&
X-Cache: MISS
Cache-Control: max-age=3
HTTP/1.1 302 Found
Server: Tengine
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: keep-alive
at_bucketid: sbucket_-1
X-Bucket-Id: -1
Location: http://jump.taobao.com/jump?target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d1
Cache-Control:
HTTP/1.1 302 Found
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: close
Set-Cookie: _tb_token_=ktbzEwzFR6qy;domain=.taobao.com;Path=/;HttpOnly
Set-Cookie: cookie2=6c6bc65b9e9a5159cff5b3d0cae4dfd9;domain=.taobao.com;Path=/;HttpOnly
Set-Cookie: t=d768c73859b40e10ef81f7abd0824704;domain=.taobao.com;Expires=Thu, 29-May-2014 06:16:01 GMT;Path=/
P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'
Location: http://pass.tmall.com/add?_tb_token_=ktbzEwzFR6qy&cookie2=6c6bc65b9e9a5159cff5b3d0cae4dfd9&t=d768c73859b40e10ef81f7abd0824704&target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d1&pacc=A_12nKe89qHIcAyauBtovg==&opi=110.75.118.230&tmsc=1393568161483632
HTTP/1.1 302 Found
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: close
P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'
Set-Cookie: _tb_token_=ktbzEwzFR6qy;domain=.tmall.com;Path=/
Set-Cookie: cookie2=6c6bc65b9e9a5159cff5b3d0cae4dfd9;domain=.tmall.com;Path=/
Set-Cookie: t=d768c73859b40e10ef81f7abd0824704;domain=.tmall.com;Path=/
Location: http://detail.tmall.com/item.htm?id=25823396605&&tbpm=1
HTTP/1.1 302 Found
Server: Tengine
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: keep-alive
at_bucketid: sbucket_-1
X-Bucket-Id: -1
Location: http://jump.taobao.com/jump?target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d2
Cache-Control:
HTTP/1.1 302 Found
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: close
Set-Cookie: _tb_token_=GgU93fEjKGT4;domain=.taobao.com;Path=/;HttpOnly
Set-Cookie: cookie2=ef3c440d74ff391de6b560da4ef8a5c9;domain=.taobao.com;Path=/;HttpOnly
Set-Cookie: t=187a71d8df58caac2c4e08d40245c31f;domain=.taobao.com;Expires=Thu, 29-May-2014 06:16:01 GMT;Path=/
P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'
Location: http://pass.tmall.com/add?_tb_token_=GgU93fEjKGT4&cookie2=ef3c440d74ff391de6b560da4ef8a5c9&t=187a71d8df58caac2c4e08d40245c31f&target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d2&pacc=Vo8f-VlYEYPJ6WE3iTX96Q==&opi=110.75.118.230&tmsc=1393568161501736
HTTP/1.1 302 Found
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: close
P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'
Set-Cookie: _tb_token_=GgU93fEjKGT4;domain=.tmall.com;Path=/
Set-Cookie: cookie2=ef3c440d74ff391de6b560da4ef8a5c9;domain=.tmall.com;Path=/
Set-Cookie: t=187a71d8df58caac2c4e08d40245c31f;domain=.tmall.com;Path=/
Location: http://detail.tmall.com/item.htm?id=25823396605&&tbpm=2
HTTP/1.1 302 Found
Server: Tengine
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: keep-alive
at_bucketid: sbucket_-1
X-Bucket-Id: -1
Location: http://jump.taobao.com/jump?target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d3
Cache-Control:
HTTP/1.1 302 Found
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: close
Set-Cookie: _tb_token_=Uta86PoG6cWC;domain=.taobao.com;Path=/;HttpOnly
Set-Cookie: cookie2=cd06dba05f2bf1124200861d0b8a151b;domain=.taobao.com;Path=/;HttpOnly
Set-Cookie: t=618388c77aff03aec08309747a82f440;domain=.taobao.com;Expires=Thu, 29-May-2014 06:16:01 GMT;Path=/
P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'
Location: http://pass.tmall.com/add?_tb_token_=Uta86PoG6cWC&cookie2=cd06dba05f2bf1124200861d0b8a151b&t=618388c77aff03aec08309747a82f440&target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d3&pacc=RptvnJCXDG4WtvFa0JIqoQ==&opi=110.75.118.230&tmsc=1393568161510003
HTTP/1.1 302 Found
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html
Content-Length: 260
Connection: close
P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'
Set-Cookie: _tb_token_=Uta86PoG6cWC;domain=.tmall.com;Path=/
Set-Cookie: cookie2=cd06dba05f2bf1124200861d0b8a151b;domain=.tmall.com;Path=/
Set-Cookie: t=618388c77aff03aec08309747a82f440;domain=.tmall.com;Path=/
Location: http://detail.tmall.com/item.htm?id=25823396605&&tbpm=3
HTTP/1.1 200 OK
Server: Tengine
Date: Fri, 28 Feb 2014 06:16:01 GMT
Content-Type: text/html;charset=GBK
Connection: keep-alive
Vary: Accept-Encoding
at_bucketid: sbucket_-1
X-Bucket-Id: -1
Cache-Control: max-age=1
At_Autype: 4_103979994
At_Cat: item_50018599
X-Category: /cat/50008090
At_Nick: %E8%B6%8A%E5%BA%A6%E6%95%B0%E7%A0%81%E4%B8%93%E8%90%A5%E5%BA%97
At_Itemid: 25823396605
At_Isb: 1
At_Pgty: 2
At_Cat: 50018599
At_Brid: 272676782
At_Prid: 221236332
At_Autype: 0_103979994
At_Auid: 25823396605
Content-Language: zh-CN
X-Cache: MISS TCP_MISS dirn:-2:-2
Via: wagbridge010207087016.cm3:8888
五、發送表單信息 發送表單信息有GET和POST兩種方法。
GET方法相對簡單,只要把數據附在網址后面就行:
curl baidu.com/form.cgi?data=xxx
POST方法必須把數據和網址分開,例如:
curl -d "method=searchone&module=seller&user_name=wb-liqiu&nickname=dd" -H"Host:fmp.view.lz.taobao.com" "10.235.160.141:8082/api.php"
六、文件上傳 假定文件上傳的表單你可以用curl這樣上傳文件,命令:
curl --form upload=@localfilename --form press=OK http://www.baidu.com
七、Referer字段 有時你需要在http request頭信息中,提供一個referer字段,表示你是從哪里跳轉過來的:
curl --referer http://www.baidu.com http://www.baidu.com
八、User Agent字段 這個字段是用來表示客戶端的設備信息。服務器有時會根據這個字段,針對不同設備,返回不同格式的網頁,比如手機版和桌面版。 iPhone4的User Agent是: “Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7”。curl可以這樣模擬: curl --user-agent "[User Agent]" [URL]
九、cookie 使用--cookie參數,可以讓curl發送cookie。
curl --cookie "name=xxx" www.baidu.com 至于具體的cookie的值,可以從http response頭信息的Set-Cookie字段中得到。也可以例如:
curl -c ./cookie.txt http://www.baidu.com
十、增加頭信息 有時需要在http request之中,自行增加一個頭信息。--header參數就可以起到這個作用。
curl --header "xxx: xxxxxx" http://baidu.com
十一、HTTP認證 有些網域需要HTTP認證,這時curl需要用到--user參數:
curl --user name:password baidu.com
<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html>十二、設置代理緩存 -x<ip:port>
curl -L -x127.0.0.1:13128 http://detail.tmall.com/item.htm?id=25823396605下載單個文件,默認將輸出打印到標準輸出中(STDOUT)中
curl http://www.centos.org
通過-o/-O選項保存下載的文件到指定的文件中:
-o:將文件保存為命令行中指定的文件名的文件中
-O:使用URL中默認的文件名保存文件到本地
# 將文件下載到本地并命名為mygettext.html
curl -o mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html
# 將文件保存到本地并命名為gettext.html
curl -O http://www.gnu.org/software/gettext/manual/gettext.html
同樣可以使用轉向字符">"對輸出進行轉向輸出
同時獲取多個文件
curl -O URL1 -O URL2
若同時從同一站點下載多個文件時,curl會嘗試重用鏈接(connection)。
通過-L選項進行重定向
默認情況下CURL不會發送HTTP Location headers(重定向).
當一個被請求頁面移動到另一個站點時,會發送一個HTTP Loaction
header作為請求,然后將請求重定向到新的地址上。
例如:訪問google.com時,會自動將地址重定向到google.com.hk上。
curl http://www.google.com
<HTML>
<HEAD>
<meta http-equiv="content-type" content="text/html;charset=utf-">
<TITLE> Moved</TITLE>
</HEAD>
<BODY>
<H> Moved</H>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=amp;usg=AFQjCNFoumfjyJpNDPuFKTibavEaA">here</A>.
</BODY>
</HTML>
這是可以通過使用-L選項進行強制重定向
1 # 讓curl使用地址重定向,此時會查詢google.com.hk站點
2 curl -L http://www.google.com
斷點續傳
通過使用-C選項可對大文件使用斷點續傳功能,如:
# 當文件在下載完成之前結束該進程
$ curl -O http://www.gnu.org/software/gettext/manual/gettext.html
############## .%
# 通過添加-C選項繼續對該文件進行下載,已經下載過的文件不會被重新下載
curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html
############### .
對CURL使用網絡限速
通過--limit-rate選項對CURL的最大網絡使用進行限制
# 下載速度最大不會超過1000B/second curl --limit-rate 1000B -O http://www.gnu.org/software/gettext/manual/gettext.html下載指定時間內修改過的文件
當下載一個文件時,可對該文件的最后修改日期進行判斷,如果該文件在指定日期內修改過,就進行下載,否則不下載。該功能可通過使用-z選項來實現:
# 若yy.html文件在2011/12/21之后有過更新才會進行下載 curl -z 21-Dec-11 http://www.example.com/yy.html
CURL授權
在訪問需要授權的頁面時,可通過-u選項提供用戶名和密碼進行授權
curl -u username:password URL # 通常的做法是在命令行只輸入用戶名,之后會提示輸入密碼,這樣可以保證在查看歷史記錄時不會將密碼泄露4 curl -u username URL
從FTP服務器下載文件
CURL同樣支持FTP下載,若在url中指定的是某個文件路徑而非具體的某個要下載的文件名,CURL則會列出該目錄下的所有文件名而并非下載該目錄下的所有文件
# 列出public_html下的所有文件夾和文件 curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/ # 下載xss.php文件 curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php上傳文件到FTP服務器
通過 -T 選項可將指定的本地文件上傳到FTP服務器上
# 將myfile.txt文件上傳到服務器
curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com
# 同時上傳多個文件
curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com
# 從標準輸入獲取內容保存到服務器指定的文件中
curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt
獲取更多信息
通過使用 -v 和 -trace獲取更多的鏈接信息
保存與使用網站cookie信息
# 將網站的cookies信息保存到sugarcookies文件中curl -D sugarcookies http://localhost/sugarcrm/index.php # 使用上次保存的cookie信息 curl -b sugarcookies http://localhost/sugarcrm/index.php傳遞請求數據
默認curl使用GET方式請求數據,這種方式下直接通過URL傳遞數據
可以通過 --data/-d 方式指定使用POST方式傳遞數據
# GET curl -u username https://api.github.com/user?access_token=XXXXXXXXXX
# POST curl -u username --data "param=value?m=value" https://api.github.com
# 也可以指定一個文件,將該文件中的內容當作數據傳遞給服務器端
curl --data @filename https://github.api.com/authorizations
注:默認情況下,通過POST方式傳遞過去的數據中若有特殊字符,首先需要將特殊字符轉義再傳遞給服務器端,如value值中包含有空格,則需要先將空格轉換成%20,如:
curl -d "value%201" http://hostname.com
在新版本的CURL中,提供了新的選項 --data-urlencode,通過該選項提供的參數會自動轉義特殊字符。
curl --data-urlencode "value 1" http://hostname.com
除了使用GET和POST協議外,還可以通過 -X 選項指定其它協議,如:
curl -I -X DELETE https://api.github.cim
上傳文件
curl --form "fileupload=@filename.txt" http://hostname/resource
1、抓取cokkie信息到一個文件中
curl -c cookie0.txt -d "username=****&password=***" http://www.kuaipan.cn/accounts/login/
2、get方法獲取信息
curl -G -I -o xsrf.txt -b cookie1.txt -c cookie2.txt
?
顯示下載進度條# curl -# -O http://www.linux.com/dodo1.JPG
7:不會顯示下載進度信息
# curl -s -O http://www.linux.com/dodo1.JPG
新聞熱點
疑難解答