脚本_从网页上抓取并下载图片

Published by davidliu on

需求:

从网页上抓取并下载图片

脚本内容如下:

#!/bin/bash

#filename:img_donwloader.sh

#function:download picture file

if [ $# -ne 3 ];

then

 echo “usage: $0 URL -d DIRECTORY”

 exit -1

fi

for i in {1..4}

do

 case $1 in

 -d) shift; directory=$1; shift;;

 *) url=${url:-$1}; shift;;

esac

done

mkdir -p $directory

baseurl=$(echo $url | egrep -o “https?://[a-z.]+”)

curl -s $url | egrep -o “<img src=[^>]*>” |

sed ‘s/<img src=\”\([^”]*\).*/\1/g’ > /tmp/$$.list

sed -i “s|^/|$baseurl/|” /tmp/$$.list

cd $directory;

while read filename;

do

 curl -s -O “$filename” –silent

done < /tmp/$$.list

使用方法:

./img_downloader.sh URL -d DIR

示例:

./img_downloader.sh http://www.betterit360.com/?p=171 -d images

查看下载的图片文件:

ll images

Categories: Uncategorized

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *