查看文章
 
Linux下的抓书工具
2007-06-10 14:44
最新代码见此页, sourceforge上代码由于我这里访问速度原因已经停止更新!!! (2008.8.26)

此工具纯属无心之作,因为我自己比较喜欢看一些门户网站读书频道上连载的历史书,却懒得一页一页的翻Web,这样不但速度比较慢,而且还要看花里胡哨的网站广告.于是用shell脚本自己写了一个抓书的东东,share出来,相信肯定有热爱读书又买不起书的书生同好们需要的:)
本贴文字数有限制,本文还有另外一部分说明在 Linux下的抓书工具2 点击此链接进入

新浪(Sina)读书频道

#!/bin/bash

rm $3.txt

echo
echo "START"

i="1"
M="$[$2+1]"

while [ $i -lt $M ]
do
echo "Downloading Chapter $i..."
wget -p -q -nd $1/$i.shtml
cat ./$i.shtml | grep "<p>" > $i.txt
sed -i 's/<p>//g' $i.txt
sed -i 's/<\/p>/\
/g' $i.txt
sed -i 's/<[^<]*>//g' $i.txt
sed -i '/[:space:][^C]*/d' $i.txt
sed -i 's/^[[:space:]]*//g' $i.txt
cat $i.txt >> $3.txt
rm $i.txt
rm $i.shtml
i=$[$i+1]
done;

echo "END"


使用方法是将上文保存为sinatest,在bash shell下chmod 755 sinatest使其具有可执行权限.

./sinatest 书的路径 最后一章的号 保存的书名

要下载的是http://book.sina.com.cn/nzt/history/his/shijixindu/index.shtml上介绍的书,一共有384章,那么就这样使用:
./sinatest http://book.sina.com.cn/nzt/history/his/shijixindu 384 shiji


2008.8.26日更新,最近新浪读书进行了改版,出现了一些vip阅读章节,如
http://vip.book.sina.com.cn/book/index_69058.html


新写了一段代码用于这些书籍的下载阅读:

#!/bin/bash

rm $2.txt
echo
echo "START"

wget -r -l 1 -A "chapter_*.html" -p -q -nd $1
cat *.html >> $2.txt
rm *.html
cat $2.txt | grep "<p>" >> $2
mv $2 $2.txt

sed -i 's/<p>//g' $2.txt
sed -i 's/<\/p>/\
/g' $2.txt
sed -i 's/^[[:space:]]*//g' $2.txt

echo "END"


使用方法是将上文保存为vipsinatest,在bash shell下chmod 755 sinatest使其具有可执行权限.
使用方法是(注意这里不需要设置章节数):
./vipsinatest http://vip.book.sina.com.cn/book/index_69058.html nvren
最后书会保存为nvren.txt

搜狐读书频道


#!/bin/bash

rm $2.txt
echo
echo "START"

wget -r -l 1 -A "chapter-*.html" -p -q -nd $1
#用ls -v排序
cat `ls -v *.html` >> $2.txt
rm *.html
cat $2.txt | grep "<p>&nbsp;&nbsp;&nbsp;&nbsp;" >> $2
mv $2 $2.txt

sed -i 's/<[^<]*>/\
/g' $2.txt
sed -i 's/\&nbsp;/\
/g' $2.txt
sed -i '/^$/d;G' $2.txt

echo "END"


保存为sohutest
要下载的是http://lz.book.sohu.com/serialize.php?id=10459上介绍的书
使用方法是 (注意这里不需要设置章节数):
./sohutest http://lz.book.sohu.com/serialize.php?id=10459 fayi
最后书会保存为fayi.txt


腾讯(QQ)读书频道

#!/bin/bash

rm $3.txt

echo
echo "START"

i="1"
M="$[$2+1]"

while [ $i -lt $M ]
do
echo "Downloading Chapter $i..."
wget -p -q -nd -A "*.shtml" $1/$i.shtml
cat ./$i.shtml | grep "<br /><br />" > $i.txt
sed -i 's/<[^<]*>//g' $i.txt
cat $i.txt >> $3.txt
echo >> $3.txt
rm $i.txt
rm $i.shtml
i=$[$i+1]
done;

echo "END"

保存为qqtest
要下载的是http://book.qq.com/s/book/0/10/10165上介绍的书
使用方法是:./qqtest http://book.qq.com/s/book/0/10/10165 20 hk
最后书会保存为hk.txt

雅虎(Yahoo)中国读书频道

#!/bin/bash

rm $2.txt
echo
echo "START"

wget -r -l 1 -A "*.html" -p -q -nd $1
cat *.html >> $2.txt
rm *.html
cat $2.txt | grep "<p>" >> $2
mv $2 $2.txt

sed -i 's/<p>//g' $2.txt
sed -i '/<p\ /d' $2.txt
sed -i 's/<[^<]*>//g' $2.txt
sed -i '/\&nbsp;/d' $2.txt

echo "END"

保存为yahootest
要下载的是http://cn.news.yahoo.com/reading/cf319qjxc/index.html上介绍的书
使用方法是(注意这里不需要设置章节数):
./yahootest http://cn.news.yahoo.com/reading/cf319qjxc/index.html lcy
最后书会保存为lcy.txt

MSN中国读书频道

#/bin/bash

rm $2.txt
echo
echo "START"

wget -r -l 1 -A "*.html,*.shtml" -p -q -nd $1
rm *.html
cat *.shtml >> $2.txt
rm *.shtml

cat $2.txt | grep "<br>" > $2
mv $2 $2.txt

sed -i 's/<br>/\
/g' $2.txt
sed -i 's/<[^<]*>//g' $2.txt
sed -i 's/\&nbsp;//g' $2.txt

echo "END"

保存为msntest
要下载的是http://msn.hongxiu.com/n/a/42385/上介绍的书
使用方法是 (注意这里不需要设置章节数,最后要加上一个/):
./msntest http://msn.hongxiu.com/n/a/42385/ kongzi
最后书会保存为kongzi.txt

猫扑(Mop)读书频道[该网站转型为论坛,因此代码作废]

#!/bin/bash

rm $2.txt
echo
echo "START"

wget -r -l 1 -A "*.html" -p -q -nd $1
cat *.html >> $2.txt
rm *.html

cat $2.txt | grep "<br>" > $2
mv $2 $2.txt

sed -i 's/\&nbsp;//g' $2.txt
sed -i 's/<br>/ /g' $2.txt
sed -i 's/<[^<]*>//g' $2.txt

echo "END"

保存为moptest
要下载http://book.mop.com/ebook/NqWx5mdz.html上介绍的书
使用方法是 (注意这里不需要设置章节数):
./moptest http://book.mop.com/ebook/NqWx5mdz.html hxd
最后书会保存为hxd.txt


Wuhan读书频道

#!/bin/bash

rm $3.txt
i="1"
M="$[$2+1]"
ten="10"

echo
echo "START"

while [ $i -lt $M ]
do
echo "Downloading Chapter $i..."
if [ $i -lt $ten ]
then
     wget -p -q -nd -A "*.htm" $1/0$i.htm
     cat ./0$i.htm | grep "<p>" > $i.txt
     rm 0$i.htm
else
     wget -p -q -nd -A "*.htm" $1/$i.htm
     cat ./$i.htm | grep "<p>" > $i.txt
     rm $i.htm
fi

sed -i '/\&nbsp;/d' $i.txt
sed -i 's/<p>//g' $i.txt
sed -i 's/<\/p>/\
/g' $i.txt
sed -i 's/<[^<]*>//g' $i.txt
sed -i 's/^[[:space:]]*//g' $i.txt
cat $i.txt >> $3.txt
rm $i.txt
i=$[$i+1]
done;

echo "BYEBYE"


保存为wuhantest
要下载的是http://book.wuhan.net.cn/xh/gcd1/gcd1.htm上介绍的书
使用方法是(注意这里地址的变化,需要设置章节数):
./wuhantest http://book.wuhan.net.cn/xh/gcd1 60 gcd1
最后书会保存为gcd1.txt

中华网(China)读书频道

#!/bin/bash
echo
echo "START"

rm $2.txt
wget -r -l 1 -A "*.html" -p -q -nd $1
rm index.html
rm weeksortbook.html

sed -i 's/<br>/\
/g' *.html
cat *.html >> $2.txt
rm *.html

sed -i 's/\&nbsp;//g' $2.txt
sed -i 's/<[^<]*>//g' $2.txt

echo "END"

保存为chinatest
要下载的是http://lianzai.culture.china.com/books/html/2189/index.html上介绍的书
使用方法是(注意这里不需要设置章节数):
./chinatest http://lianzai.culture.china.com/books/html/2189/index.html gjm
最后书会保存为gjm.txt

美人鱼小说阅读网

#!/bin/bash
rm $2.txt
echo "START"

wget -r -l 1 -A "*.htm" -p -q -nd $1
cat *.htm >> $2.txt
rm *.htm

sed -i '/<[^<]*>/d' $2.txt
sed -i '/--/d' $2.txt
sed -i '/font/d' $2.txt
sed -i '/color/d' $2.txt
sed -i '/text/d' $2.txt
sed -i '/page/d' $2.txt

echo "END"

保存为renyutest
要下载的是http://www.renyu.net/wxxs/j/jingyong/ffwz/index.html上介绍的书
使用方法是(注意这里不需要设置章节数):
./renyutest http://www.renyu.net/wxxs/j/jingyong/ffwz/index.html fh
最后书会保存为fh.txt

小说阅读网

#!/bin/bash

rm $3.txt

echo
echo "START"

i="1"
M="$[$2+1]"

while [ $i -lt $M ]
do
echo "Downloading Chapter $i..."
wget --header="User-Agent: Mozilla/4.0" -A "*.html" -p -q -nd $1/$i.html
cat ./$i.html | grep "<P>" > $i.txt
sed -i 's/<P>//g' $i.txt
sed -i 's/<\/P>/\
/g' $i.txt
sed -i 's/<BR>//g' $i.txt
sed -i 's/<[^<]*>//g' $i.txt
sed -i '/[:space:][^C]*/d' $i.txt
sed -i 's/^[[:space:]]*//g' $i.txt
cat $i.txt >> $3.txt
rm $i.txt
rm $i.html
rm leftads4.html
i=$[$i+1]
done;

echo "END"


保存为readnoveltest
要下载的是http://www.readnovel.com/novel/26297.html上介绍的书
使用方法是(注意地址的变化,需要加上下载的章节数,章节数可以通过http://www.readnovel.com/partlist/26297/上书目链接查到)
./readnoveltest http://www.readnovel.com/novel/26297 98 kangri
最后书会保存为kangri.txt

百度国学

#!/bin/bash

rm $3.txt

echo
echo "START"

i="1"
M="$[$2+1]"

while [ $i -lt $M ]
do
echo "Downloading Chapter $i..."
wget -A "*.html" -p -q -nd $1/$i.html
cat ./$i.html | grep "<br>" > $i.txt
sed -i 's/<br>/\
/g' $i.txt
sed -i 's/<[^<]*>//g' $i.txt
sed -i 's/\&nbsp;//g' $i.txt
sed -i 's/^[[:space:]]*//g' $i.txt
cat $i.txt >> $3.txt
rm $i.txt
rm $i.html
i=$[$i+1]
done;

echo "END"

保存为baidutest
要下载的是http://guoxue.baidu.com/page/b4bac7efd7f3b4ab/index.html上介绍的书
使用方法是(注意地址的变化,需要加上下载的章节数,章节数可以通过http://guoxue.baidu.com/page/b4bac7efd7f3b4ab/index.html上书目链接查到)
./baidutest http://guoxue.baidu.com/page/b4bac7efd7f3b4ab 11 zuozhuan
最后书会保存为zuozhuan.txt

网易文化频道-读书

#!/bin/bash

rm $2.txt

echo
echo "START"

wget -r -l 1 -A "*24.html" -p -q -nd $1
rm special.html
cat *.html | grep "<P style" >> $2.txt
rm *.html

sed -i 's/<\/P>/\
/g' $2.txt
sed -i 's/<[^<]*>//g' $2.txt


echo "END"

保存为163test
要下载的是http://culture.163.com/special/00281M4U/0611xinjiehun.html上介绍的书
使用方法是(注意这里不需要设置章节数)
./163test http://culture.163.com/special/00281M4U/0611xinjiehun.html xjhsd
最后书会保存为xjhsd.txt

亦凡公益图书馆

#!/bin/bash

rm $2.txt

echo
echo "START"

wget -r -l 1 -A "*.html" -p -q -nd $1
cat *.html >> $2.txt
rm robots.txt
rm robots.txt.*
rm *.html

sed -i '/</d' $2.txt
sed -i '/\&nbsp;/d' $2.txt
sed -i '/initializeimg/d' $2.txt
sed -i '/{/d' $2.txt
sed -i '/>/d' $2.txt
sed -i '/}/d' $2.txt
sed -i '/var/d' $2.txt


echo "END"

保存为shukutest
要下载的是http://www.shuku.net:8082/novels/dangdai/hongfennvlang/hongfennvlang.html上介绍的书
使用方法是(注意这里不需要设置章节数)
./shukutest http://www.shuku.net:8082/novels/dangdai/hongfennvlang/hongfennvlang.html fhnl
最后书会保存为fhnl.txt

---------------------------------------------
相关代码可以在Sourceforge上下载,地址是:http://sourceforge.net/projects/zhuashushell

声明:不要用这个东东干坏事,印刷盗版书籍哦:)

类别:Open Source||添加到搜藏 |分享到i贴吧|浏览(12073)|评论 (0)
 
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu