使用bash编写图片爬虫

2019-04-15 16:57发布

#!/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
============================================================================================= 执行结果