暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

教你如何用shell 脚本爬取网站的图片

齐浩的运维笔记 2019-03-05
550


最近在看《Linux-Shell脚本攻略》一书,书中有个图片抓取器的script,抓取出来记录一下。有兴趣的可以自己尝试尝试


用法:复制以下代码,命名为img_downloader.sh。使用时在shell下输入 ./img_downloader.sh www.baidu.com -d images,该shell脚本就会把百度首页上的图片下下来了。



#!/bin/bash


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.]+")

echo

"$baseurl"


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
文章转载自齐浩的运维笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论