這篇文章主要介紹了shell腳本如何實(shí)現(xiàn)獲取圖片,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)專注于鞏義企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。鞏義網(wǎng)站建設(shè)公司,為鞏義等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
shell腳本實(shí)現(xiàn)獲取圖片。
#!/usr/bin/bash
. /etc/init.d/functions
function check(){
time=`date "+%F %X"`
echo "當(dāng)前時(shí)間:$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 "網(wǎng)絡(luò)異常,請檢查"
else
echo_success && echo "網(wǎng)絡(luò)正常 當(dāng)前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個(gè)"
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 "下載異常,正在清理環(huán)境"
rm -rf $2
return 101
fi
}
clear
echo -e "本程序功能為獲取圖片: 圖片\n禮貌爬取,快樂你我他\n程序設(shè)計(jì)目的:shel腳本#!/usr/bin/bash
. /etc/init.d/functions
function check(){
time=`date "+%F %X"`
echo "當(dāng)前時(shí)間:$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 "網(wǎng)絡(luò)異常,請檢查"
else
echo_success && echo "網(wǎng)絡(luò)正常 當(dāng)前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個(gè)"
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 "下載異常,正在清理環(huán)境"
rm -rf $2
return 101
fi
}
clear
echo -e "本程序功能為獲取圖片: 快樂你我他\n程序設(shè)計(jì)腳本測
sleep 1
read -p "請輸入要爬取的圖片關(guān)鍵字,默認(rèn)為中國): " words
read -p "請輸入資源存放路徑,默認(rèn)為 /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
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“shell腳本如何實(shí)現(xiàn)獲取圖片”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!