在線教程一般像流水線一樣,頁面有上一頁下一頁的按鈕,因此,可以利用shell寫一個爬蟲讀取下一頁鏈接地址,配合wget將教程所有內容抓取。
以postgresql中文網為例。下面是實例代碼
#!/bin/sh
start_URL="http://www.postgres.cn/docs/9.6/preface.html"
end_URL="http://www.postgres.cn/docs/9.6/bookindex.html"
URL=$start_URL
while [ $URL != $end_URL ];do
curl -s $URL >tmp.txt
wget $URL -P psql
grep -n 'ACCESSKEY="N"' tmp.txt > tmp2.txt
cut -f1 -d":" tmp2.txt | head -n 1 > tmp3.txt
let LINE=`cat tmp3.txt`
let LINE--
sed -n "${LINE}p" tmp.txt > tmp4.txt
sed -i 's/HREF="//g' tmp4.txt
sed -i 's/"//g' tmp4.txt
sURL=`cat tmp4.txt`
cat tmp4.txt >> allurl.txt
FULLURL="http://www.postgres.cn/docs/9.6/$sURL"
URL=$FULLURL
done
rm -rf tmp.txt tmp2.txt tmp3.txt tmp4.txt
說明:
1、URL 要下載的html文件路徑
2、sURL html文件的相對路徑
3、FULLURL sURL和模板拼接后的完整url
4、tmp.txt 用於保存curl取得的頁面數據
