shell获取网页信息并保存到数据库中

#!/bin/bash
#
wget -nd -p -o tianya.html http://www.tianya.cn/techforum/articleslist/0/414.shtml
#
获得网页信息
function GetWebContent(){
iconv -f gb2312 414.shtml|awk '
BEGIN { FS="target=\"_blank\">";}{
if(/更新时间/){
flag = "true";
}
if(flag == "true"){
if(/<a/){
print $2;
}
}
if(/class="ftarea"/){
flag = "false"
}
}
' | awk '
BEGIN {FS="</a>" }
{
print $1;
}
' > result.txt
}
#将信息插入数据库
function insertIntoDataBase(){
i=0
while read myline
do
arry[$i]=$myline
i=`expr $i + 1`
done < result.txt

j=0
while [ `expr $j \* 2` -le $i ]
do
sqlite3 news.db << !
insert into tbNew(title,author) values ("${arry[`expr $j \* 2`]}","${arry[`expr $j \* 2 + 1`]}");
!
j=`expr $j + 1`
done

echo 'select *from tbNew;'|sqlite3 news.db|awk 'BEGIN{FS="|"} {print "No",$1,"tilte",$2,"author",$3}'| cat > t.txt ;

}
#主函数
GetWebContent
insertIntoDataBase
原文地址:https://www.cnblogs.com/wuxi/p/2284682.html