因为看见老男孩的学员做过这一期,也很有兴趣,并且刚在学python,所以用python实现了下。由于刚学习python,肯定还有很多地方可以优化。
#!/bin/bash # oldboy linux training # 2015-06-01 # Happy Children's Day # 说明:本脚本来自老男孩linux21期学员张耀开发! EduFile=/tmp/edu.html EduFile2=/tmp/edu2.html 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 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 } # Defined Create table Functions function table(){ sum="" index=1 sed -rn '/do=lesson/ s#<.*($#\1http://edu.51cto.com\2#gp' $EduFile > $EduFile2 while read line do sum=$sum"$index $line " ((index++)) done <$EduFile2 } # Defined Create html Functions function html(){ cat >/tmp/oldboy.html<<-ENDtest $sum