也不能算是爬虫吧,只是个简单的下载图片的脚本。

# only for lolibooru.moe ver.2
# https://lolibooru.moe/post?page=${max}&tags=${tag}

down="wget -c -w 10 -t 3"

show_usage() {
    echo "Usage: $0 <tag> [max]"
}

error() {
    echo "$@" 1>&2
}

download() {
    #top_post=$@

    for post in $top_post; do
        echo "searching in post[$post] at page[$i]"
        post=$(echo $post | cut -c 8-)
        post="https://$post"
        pic=$(curl -s $post | grep -Eo 'https?\:\/\/lolibooru\.moe\/(jpeg|image)\/.+\.(jpg|png|gif)' | sort -u)
        if [ "$pic" == ""  ]; then
            error "W: not found pic in [$post]";
            continue
        fi

        $down "$pic" || error "E: can not download [$pic]";
    done
}

search() {
    tag=$1
    max=$2

    if [ "$tag" == "" ]; then
        error "tag is empty";
        exit 1
    fi

    for i in $(seq 1 ${max}); do
        url="https://lolibooru.moe/post?page=${i}&tags=${tag}"
        top_post=$(curl -s $url | grep -Eo 'https?\:\/\/lolibooru.moe\/post\/show\/[0-9]{6}')
        download "$top_post";
    done
}

main() {
    case $1 in
    -h|--help)
        show_usage;
    ;;
    *)
        search $1 $2;
    ;;
    esac
}

main $@;

# vim: set ts=4 sw=4 et:

使用方法

bash <脚本名字> <标签> <页数>

bash l.sh loli 10