这篇文章主要介绍了shell脚本如何实现获取图片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
shell脚本实现获取图片。
#!/usr/bin/bash
. /etc/init.d/functions
function check(){
time=`date "+%F %X"`
echo "当前时间:$time"
ip=$(ip a |grep inet |grep "scope global dynamic"|awk -F"/" '{print $1}' |awk '{print $2}')
ping -w1 -c1 www.baidu.com &>/dev/null
if [[ $? -ne 0 ]];then
echo_failure || echo "网络异常,请检查"
else
echo_success && echo "网络正常 当前IP:$ip"
fi
yum repolist &>/dev/null
if [[ $? -ne 0 ]];then
echo_failure || echo "yum异常,请检查"
fi
}
function down(){
yum install -y wget &>/dev/null
w=`echo "$1" |tr -d '\n' |od -An -tx1|tr ' ' %`
data=$(curl -s "https://image.baidu.com/search/index?tn=baiduimage&word=$w")
echo $data | grep -P -o "https://ss.*?.jpg" | sort -n -r |uniq >> $2/url2.txt
a=0
while read line
do
sleep 1
code=$(curl -I "$line" 2>/dev/null)
if [ ${code:9:3} -eq 200 ];then
wget -O $2/$1${a}.jpg $line &>/dev/null
echo "下载完成 $line"
else
echo "无效资源 $line"
fi
let a++
done <$2/url2.txt
echo "全部下载完成,下载资源$a个"
return 200
}
function clea(){
echo "正在打包,请稍后"
sleep 2
tar cvzf image.tar.gz $1
rm -rf $1/url2.txt
}
function main(){
check
down $1 $2
if [[ $? -eq 200 ]];then
clea $2
return 100
else
echo "下载异常,正在清理环境"
rm -rf $2
return 101
fi
}
clear
echo -e "本程序功能为获取图片: 图片\n礼貌爬取,快乐你我他\n程序设计目的:shel脚本#!/usr/bin/bash
. /etc/init.d/functions
function check(){
time=`date "+%F %X"`
echo "当前时间:$time"
ip=$(ip a |grep inet |grep "scope global dynamic"|awk -F"/" '{print $1}' |awk '{print $2}')
ping -w1 -c1 www.baidu.com &>/dev/null
if [[ $? -ne 0 ]];then
echo_failure || echo "网络异常,请检查"
else
echo_success && echo "网络正常 当前IP:$ip"
fi
yum repolist &>/dev/null
if [[ $? -ne 0 ]];then
echo_failure || echo "yum异常,请检查"
fi
}
function down(){
yum install -y wget &>/dev/null
w=`echo "$1" |tr -d '\n' |od -An -tx1|tr ' ' %`
data=$(curl -s "https://image.baidu.com/search/index?tn=baiduimage&word=$w")
echo $data | grep -P -o "https://ss.*?.jpg" | sort -n -r |uniq >> $2/url2.txt
a=0
while read line
do
sleep 1
code=$(curl -I "$line" 2>/dev/null)
if [ ${code:9:3} -eq 200 ];then
wget -O $2/$1${a}.jpg $line &>/dev/null
echo "下载完成 $line"
else
echo "无效资源 $line"
fi
let a++
done <$2/url2.txt
echo "全部下载完成,下载资源$a个"
return 200
}
function clea(){
echo "正在打包,请稍后"
sleep 2
tar cvzf image.tar.gz $1
rm -rf $1/url2.txt
}
function main(){
check
down $1 $2
if [[ $? -eq 200 ]];then
clea $2
return 100
else
echo "下载异常,正在清理环境"
rm -rf $2
return 101
fi
}
clear
echo -e "本程序功能为获取图片: 快乐你我他\n程序设计脚本测
sleep 1
read -p "请输入要爬取的图片关键字,默认为中国): " words
read -p "请输入资源存放路径,默认为 /tmp/image : " path
if [ -z $words ];then
words="中国"
fi
if [ -z $path ];then
path='/tmp/image'
mkdir /tmp/image
fi
main $words $path
if [ $? -eq 100 ];then
echo "程序退出"
else
echo "程序异常退出"
fi
感谢你能够认真阅读完这篇文章,希望小编分享的“shell脚本如何实现获取图片”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。