使用bash編寫圖片爬蟲

#!/bin/bash
LINKFORMAT="http://img1.mm131.com/pic/%d/%d.jpg"
MAX_COUONT=100


#download mm gallery,to stop if can't download
function download_gallery(){
    if [ ! -d "$1" ];then
        #if create dir not sucess,so do it
        if `mkdir "$1"`;then
            #empty statement using :
            :
        else
            echo "create dir fail:"$1""
            exit 2
        fi
    fi


    echo "Downloading gallery $1..."
    index=1
    url=`printf $LINKFORMAT "$1"  $index`
    echo "url=$url"
    while `wget -P "$1" "$url"`;do
        let "index+=1"
        #function call
        url=`printf $LINKFORMAT  "$1"  $index `
        echo "url=$url"
        if [ $index -ge $MAX_COUONT ];then
            echo "Gallery "$1" exceed!!!"
            break
        fi
    done
}


#crawl images
ARGCOUNT=1
if [ $# -ne $ARGCOUNT ];then
    echo "'basename ${ARGCOUNT}' mmid"
    exit 1
fi


  download_gallery "$1"
# download_gallery 2172


exit 0


=============================================================================================

執行結果




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