1. 程式人生 > >玩兒蟲那些事(四)—— 使用curl

玩兒蟲那些事(四)—— 使用curl

nod -h div sel ant validate 空間 pre rap

目錄

  • 一、爬一個簡單的網站
  • 二、模擬登錄新浪
  • 三、各種請求的發送
  • 四、使用curl
  • 五、模擬登錄QQ空間
  • 六、selenium的使用
  • 七、phantomjs的使用
  • 八、開源框架webmagic
  • 九、開源框架scrapy
  • 十、多線程爬取與反爬策略
  • 十一、加密與解密
  • 十二、APP數據抓取
  • 十三、分布式爬蟲

1、 發送一個簡單的請求,獲取頁面,並查看請求頭

curl https://www.baidu.com

curl https://www.baidu.com --head

2、 下載某一資源

curl https://www.baidu.com/1.png

3、將請求到的文件輸出到某個文件中

curl https://www.baidu.com -o baidu.txt

4、 請求需要驗證 HTTP authentication

curl -u username:password https://xxxx.com

5、 在配置中進行批量操作

curl -K cmdline.txt https://baidu.com

cmdline.txt

--location

--head

(6) 分析請求和響應,並將響應結果保存


curl -v https://baidu.com -o response.txt

執行結果如下


* Rebuilt URL to: http://www.baidu.com/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 119.75.213.61...
* TCP_NODELAY set
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to www.baidu.com (119.75.213.61) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: bfe/1.0.8.18
< Date: Thu, 18 Jan 2018 17:17:36 GMT
< Content-Type: text/html
< Content-Length: 2381
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Connection: Keep-Alive
< ETag: "588604c8-94d"
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Pragma: no-cache
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< Accept-Ranges: bytes
<
{ [2381 bytes data]
100  2381  100  2381    0     0   3770      0 --:--:-- --:--:-- --:--:--  3767
* Connection #0 to host www.baidu.com left intact

7、使用post發送數據

curl -d ‘name=amdini&passwd=123‘ http://example.com

curl -d ‘{json}‘  -H  ‘Content-Type:application/json‘ http://example.com

8、對發送數據進行編碼

curl --data-urlencode "name=John Doe(Junior)" http://example.com

請求的效果

name=John%20Doe%20%28Junior%29

9、發送multipart類型的數據

請求前的格式可能是這樣的

<form action="submit.cgi" method="post" enctype="multipart/form-data">
Name: <input type="text" name="person"><br>
File: <input type="file" name="secret"><br>
<input type="submit" value="Submit">
</form>

請求方法

可以使用-F--form

  curl -F person=anonymous -F [email protected] http://example.com/submit.cgi

請求的效果

--------------------------d74496d66958873e
Content-Disposition: form-data; name="person"
anonymous
--------------------------d74496d66958873e
Content-Disposition: form-data; name="secret"; filename="file.txt"
Content-Type: text/plain
contents of the file
--------------------------d74496d66958873e

玩兒蟲那些事(四)—— 使用curl