抓取網頁的腳本 【修復】

之前張耀老師的網頁腳本由於51cto升級,課程列表頁面改用javascript失效了

筆者發現視頻課程頁面右邊的列表都爲靜態化後的視頻課程地址,遂將老師初始版本的腳本進行了修改,在對視頻課程頁面使用時結果正常,遂將及修改後的腳本和部分註釋發上來供大家研究

[root@m01 scripts]# cat html_to_table.sh 
#!/bin/bash
# oldboy linux training
# 2016-11-13
# 基於老男孩linux21期學員張耀開發腳本
# 
EduFile=/tmp/edu.html      #process temp file1
EduFile2=/tmp/edu2.html    #process    2
Url="$*"
 
# Check for given parameters 
[ $# -eq 0 ] && {
   echo "USAGE: /bin/sh $0 http://...."
   exit 1
}
 
# Judge url is ok?
curl -I $Url &>/dev/null 
[ $? -ne 0 ] &&{
   echo "Bad url,Please check it"
   exit 1
}
 
# Defined get pagenum and CourseId Functions

# Defined get pagenum and CourseId Functions
#function getnum(){
#        curl -s $Url>$EduFile
#       grep '"pagesGoEnd"' $EduFile &>/dev/null
#        if [ $? -eq 0 ]
#          then
#            num=`sed -rn 's#.*page=([0-9].*)" class="pagesGoEnd".*$#\1#gp' $EduFile`
#        else
#            num=`sed -rn 's|.*page=([0-9].*)#" class="pagesNum".*$|\1|gp' $EduFile`
#        fi
#        pagenum=${num:-1}
#        CourseId=`echo $Url|awk -F "[-.]" '{print $4}'`
#}


# Defined curl html Functions
#function Curl(){
#        getnum
#        for i in `seq $pagenum`
#          do 
#            curl "http://edu.51cto.com/index.php?do=course&m=lessions&course_id=$CourseId&page=$i" 1>>$EduFile 2>/dev/null
#        done
#}
#分段沒了,原函數保留,視頻頁抓一遍就好
function Curl(){
            curl "$*" 1>>$EduFile 2>/dev/null
}
 
# Defined Create table Functions
function table(){
        sum=""
        index=1
        sed -rn '/lesson/ s#<.*(<a href=")(.*)#\1http://edu.51cto.com\2#gp' $EduFile > $EduFile2
        while read line
          do
            sum=$sum"<tr><th width="40" scope="row">$index</th><td width="520">$line</td>"
            ((index++))
        done <$EduFile2
}
 
# Defined Create html Functions
function html(){
        cat >/tmp/oldboy.html<<-END
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>test</title>
        </head>
        <body>
        <table width="560" border="1">
        $sum
        </table>
        </body>
        </html>
END
}
 
function main(){
        Curl
        table
        html
}
 
main


效果是相似的但是像換頁之類的功能都沒用到,還是有很多值得向前輩學習的地方

tips:cat語句特別是最後end不能有空格,前端縮進最好用tab鍵完成

源腳本 “zhangyao” 博客,請務必保留源出處http://life2death.blog.51cto.com/7550586/1657133

最後歡迎大家訪問我的wp博客>https://www.drscrewdriver.com 豈止博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章