抓取网页的脚本 【修复】

之前张耀老师的网页脚本由于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 岂止博客

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