百度空间 | 百度首页 
 
查看文章
 
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 | 添加到搜藏 | 浏览() | 评论 (8)
 
网友评论:
1
2007-11-08 20:44 | 回复
崇拜死你了@!!!!
 
2
2007-11-09 12:11 | 回复
写一个下载腾讯原创大赛的?像这里的 http://yc.qq.com/cgi-bin/disp_workintro?workid=25948 lp很喜欢看那里的小说。谢谢啦。
 
3
2007-11-09 13:04 | 回复
感谢作者! PS. 如果设定好统一的接口,整合为一个脚本就好了。 另,cat xx|grep xx, 直接用grep就行了
 
5
2007-11-10 22:15 | 回复
今天增加了对http://www.66book.net/的支持,版本为2.7,请查看<> http://hi.baidu.com/vvfang/blog/item/c8ed524a68147c2108f7efe6.html 代码可从sourceforge上下载
 
6
2007-11-11 12:56 | 回复
好样的!
 
7
2007-12-09 20:15 | 回复
这个工具非常不错阿。可是我发现一个小问题。下载新浪的图书的脚本, 比如说对这个网页(http://book.sina.com.cn/nzt/yc/guichuideng/73.shtml)分析的时候会丢失文章内容的一部分,无奈本人的水平有限,研究了半天也没有发现问题的根本所在,大概估计是在这句“sed -i 's/^[[:space:]]*//g' $i.txt”上。希望对您帮助。
 
8
2007-12-09 21:27 | 回复
如果您有空可以帮我改改,或者自己修改也成。因为Sina的格式太多了,甚至还有主URL和章节URL不同的情况,不能面面俱到,只能给大家提供一个范例,想深化的可以继续弄弄。
 
9
2007-12-11 04:02 | 回复
您好,发现几点给你报告一下,呵呵。 1 sed -i '/[:space:][^C]*/d' $i.txt sed -i 's/^[[:space:]]*//g' $i.txt 以上两句可以去掉,去掉后不仅能够不丢失内容而且能够保持原文的分段。 2 建议在文章下载下来之后马上进行编码转换,转换成utf-8(原始网页为GBK)iconv -f GBK -t utf-8,这样sed才能够无误进行工作。最初的时候我抓下来的书里面老是有“”,按道理说 sed -i 's/<[^<]*>//g' 这句能够把它去掉。折腾了半天,将字符转换成utf-8后,嘿嘿,一切ok了。 以上只是针对某篇文章有效果,对其他文章估计会有问题。也写下来供大家参考参考。
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu