05.17 網絡爬蟲|接口測試|資源獲取:使用 shell 命令獲取頭條文章

網絡爬蟲|接口測試|資源獲取:使用 shell 命令獲取頭條文章

概述

之前接口測試的時候,經常遇到從response中截取數據的情況,處理起來可能也相對簡單一點。今天換個玩法,用shell命令獲取頭條個人主頁文章。

命令如下:

$ curl https://www.toutiao.com/u/840c2172e2e3 |grep 'title' | awk -F '>' '{print $2}' | grep -v -e '
使用charles篩選、修改請求與響應
linux下MySQL常用操作(學習筆記)
隨便寫寫最近的面試
一些linux命令學習(二)--grep命令
一些linux命令學習(一)
windows下appium1.6排坑及安裝
基於python+appium+yaml安卓UI自動化測試分享
接口測試框架優化(二)---主要代碼

網絡爬蟲|接口測試|資源獲取:使用 shell 命令獲取頭條文章

詳述

實現這個目標,需要懂一些基礎的命令

  • curl

  • grep

  • awk

curl命令學習

curl命令是一個利用URL規則在命令行下工作的文件傳輸工具。它支持文件的上傳和下載,所以是綜合傳輸工具,但按傳統,習慣稱curl為下載工具。

目前我只get到了使用curl命令發送請求,暫時沒有用來上傳下載文件,所以不多做介紹。

輸入 curl -h,可以看到使用幫助

$ curl -h
Usage: curl [options...]
--abstract-unix-socket <path> Connect via abstract Unix domain socket
--anyauth Pick any authentication method
-a, --append Append to target file when uploading
--basic Use HTTP Basic Authentication
--cacert <file> CA certificate to verify peer against
--capath CA directory to verify peer against
-E, --cert <certificate> Client certificate file and password

--cert-status Verify the status of the server certificate
--cert-type <type> Certificate file type (DER/PEM/ENG)
--ciphers <list> SSL ciphers to use
--compressed Request compressed response
--compressed-ssh Enable SSH compression
.
.
./<list>/<type>/<certificate>
/<file>/<path>

就我自己實際操作的幾個介紹一下:

1.獲取頁面內容

curl https://www.toutiao.com/

不加任何選項使用 curl 時,默認會發送 GET 請求來獲取鏈接內容

2.發送POST請求

curl -H "Content-Type: application/json" -X POST
-d '{"uid": "10588666", "device_code": "", "count": 8, "op": 1, "page": 1, "content_type": [1,2,3,4], "skip_freq": 0, "exclude_docs": [], "is_wifi": 0, "is_videopage": 0, "region": 111, "register_timestamp":1519642955, "tk": "ACAWILmsfP5FV7JqM6knRK66w8j9Rqr0Aqk0NzUxNDk1MDg5NTIyNQ", "client_version":20826, "group":"exp_test_member00"}' http://localhost:2051/recommend

這邊用到了3個參數

  • H 定義請求頭 header

  • X 指定請求方式

  • d 指定發送的數據

3.顯示響應頭

curl -I https://www.toutiao.com/u/840c2172e2e3

加了-I 參數,僅顯示response header,結果如下

$ curl -I https://www.toutiao.com/u/840c2172e2e3
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0HTTP/1.1 200 OK
Date: Mon, 14 May 2018 13:05:15 GMT
Server: Tengine
Content-Type: text/html; charset=utf-8X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
ETag: W/"600753ffd703a50bfb60aca2580cddb0"Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: locale=zh-CN; path=/
Set-Cookie: _m7e_session=406d79c64df9441d376f82b2; path=/; expires=Mon, 14 May 2018 19:05:15 -0000; secure; HttpOnly
X-Request-Id: 9813d0cf-a68e-492f-8d04-580b8e149af6
X-Runtime: 0.129613Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Via: 1.1 PSfjqzdx7yx12:4 (Cdn Cache Server V2.0), 1.1 xinxiazai13:1 (Cdn Cache Server V2.0)
Connection: keep-alive
X-Dscp-Value: 0

4.保存響應的內容

curl -o D:\\jianshu.html https://www.toutiao.com/u/840c2172e2e3

執行此命令,會在D盤根目錄生成一個toutiao.html文件,如圖:

網絡爬蟲|接口測試|資源獲取:使用 shell 命令獲取頭條文章

網絡爬蟲|接口測試|資源獲取:使用 shell 命令獲取頭條文章

curl命令還有其他用法,我這邊由於暫時沒有用到,所以沒有進一步操作,感興趣可以參考這篇文章學習一下

grep命令學習

關於grep命令,我上次學習了一次,這邊就不多說了,可以參照我之前的學習筆記。

shell命令獲取文章標題中用到grep的可能就是 -v -e 兩個命令,相對還是比較簡單的。

awk命令學習

awk我也是get到一點皮毛,沒有很深入研究。大致工作流如下:

-F 分隔符劃分域,,$0則表示所有域,$1表示第一個域,$n表示第n個域。默認域分隔符是"空白鍵" 或 "[tab]鍵".

舉個例子:

D盤下新建一個文件,awk.txt。

執行命令:

$ nl awk.txt 1 root 1 name:001
2 cha 2 naaa:002
3 dong 3 nacc:003
4 fead 4 naic:004

如果只要顯示行號

$ nl awk.txt |awk '{print $1}'1
2
3
4

如果要顯示:後面的內容

$ nl awk.txt |awk -F':' '{print $2}'001
002
003
004

結束

使用shell獲取頭條主頁文章,寫法肯定不止這一種方法

$ curl https://www.toutiao.com/u/840c2172e2e3 |grep 'title' | awk -F '>' '{print $2}' | grep -v -e '

但是大致上思路是固定的:

  • 將結果用grep 篩選出需要的,排除不需要的

  • 再用awk分割選擇自己需要的域即可。

期待後面,開發出更多關於shell的玩法。。。


程序爬蟲抓取有用資源共享給大家

關注後,私信回覆【資料包】獲取如下內容,

測試資料、工具安裝、Python、效率軟件、自動化測試報告、梯子、微信群、測試框架 等


分享到:


相關文章: