用shell脚本下载小说
Tofloor
poster avatar
老陌
deepin
2018-09-29 16:44
Author
本帖最后由 myccloves 于 2018-9-29 08:46 编辑

这是老陌前一段时间写的脚本,用于下载小说。

不过现在发现阅读小说真是坑啊。一是浪费时间啊,二是眼晴受不了,三是自废武功啊……

https://bbs.deepin.org/post/169383

-----------------------------------------------------------------


最近老陌发现一个网站可以看小说。
网址:https://www.x23us.com

我想把它下载下来看,不过章节太多,好几千章,所以准备用脚本下载。

以这个小说为例:https://www.x23us.com/html/53/53495/

一、章节目录及文章下载地址
调用格式:
  1. ./contents  https://www.x23us.com/html/53/53495/ url.list
Copy the Code

目录保存到url.list中,这里除了有目录名还有对应的章节地址。

脚本代码
  1. #!/bin/bash
  2. #filename contents

  3. if [ "$#" -ne 2 ]; then
  4.         echo "contents url url.list"
  5.         exit 1
  6. fi

  7. list=$(curl $1 2>/dev/null | iconv -f gbk -t utf-8  | grep -o 'read_share.*' |  sed 's/<\/a>/\n/gi' | grep -o "[0-9]\\{5,\\}.html.*"| sed 's/\">/@/gi' | sed 's/ //gi')

  8. for i in $list; do
  9.         echo $1$i >> $2
  10. done
Copy the Code

目录列表
  1. https://www.x23us.com/html/53/53495/21602865.html@第1章:五年的坚持
  2. https://www.x23us.com/html/53/53495/21602866.html@第2章:萌芽
  3. https://www.x23us.com/html/53/53495/21602867.html@第3章:困龙升天柱
  4. https://www.x23us.com/html/53/53495/21602868.html@第4章:龙力通玄关
  5. https://www.x23us.com/html/53/53495/21602869.html@第5章:见一次打一次
  6. ...
Copy the Code

前部分是网址,后部分是标题,用@分隔。

二、通过目录列表进行下载
调用格式: download 目录列表 文章保存位置
  1. ./download url.list 1.txt
Copy the Code

脚本
  1. #!/bin/bash
  2. #filename download

  3. if [ "$#" -ne 2 ]; then
  4.         echo "download url.list filename.txt"
  5.         exit 1
  6. fi


  7. for i in `cat $1`; do
  8.         url=$(echo $i | cut -f 1 -d@)
  9.         title=$(echo $i | cut -f 2 -d@)
  10.         echo "downloading ... $url"
  11.         echo "$title" >> $2
  12.         echo "---------------------" >> $2
  13.         curl $url 2>/dev/null | iconv -f gbk -t utf-8 | grep -o "id=\"content.*"  | sed 's/ / /gi' | sed 's/
    /\n/gi'| sed 's/<\/dd>//gi' | sed 's/id=\"content.*>//gi' >> $2
  14.         echo "@" >> $2
  15.         echo "@" >> $2
  16. done
Copy the Code

生成文章
第1章:五年的坚持
---------------------
天武国石岩城叶家操练场,数十位14岁左右的孩子顶着烈日站在操练场上排着队伍,脸上皆是露出了兴奋的表情。而一些中年男女,围着广场看着自己的孩子,脸上的表情却是各一。

生成的文章,先显示标题,再显示内容。

三、加速下载
下载的过程还是比较慢的,可以把目录列表分成N分,之后用download命令同时下载N份目录列表(放到后台),这样能快很多。

调用
  1. splist url.list
Copy the Code

把目录列表拆分成1.list, 2.list ….
此时会生成runlist命令和runtxt命令,前者进行下载,后者对下载的文件合并。
  1. runlist
Copy the Code

这个运行需要一些时间, 运行完之后执行:
  1. runtxt
Copy the Code

脚本
  1. #!/bin/bash

  2. if [ "$#" -ne 1 ]; then
  3.         echo "splist url.list"
  4.         exit 1
  5. fi


  6. count=0
  7. filename=1
  8. for i in `cat $1`; do
  9.         count=$(($count+1))
  10.         if [ "$count" -eq 30 ]; then
  11.                 count=0
  12.                 filename=$(($filename+1))
  13.         fi
  14.         echo $i >> "$filename.list"
  15. done


  16. for j in `seq $filename`; do
  17.         echo "./download $j.list $j.txt &" >> runlist
  18. done

  19. for k in `seq $filename`; do
  20.         echo "cat $k.txt >> tmp.txt" >> runtxt

  21. done


  22. chmod +x runlist
  23. chmod +x runtxt
Copy the Code


四、清理工作
调用
  1. ./clean
Copy the Code

脚本
  1. #!/bin/bash

  2. cat tmp.txt  | sed '/^$/ d' | sed 's/@//' >> story.data
  3. rm *.txt
  4. rm *.list
  5. rm runlist
  6. rm runtxt
Copy the Code

五、测试
  1. ./content https://www.x23us.com/html/57/57363/ url.list
  2. ./splist url.list
  3. ./runlist  

  4. #等上面的命令在后台运行完
  5. ./runtxt
  6. ./clean
Copy the Code


Reply Favorite View the author
All Replies
avatar
whoam1
deepin
2018-09-29 23:39
#1
https://www.bequge.com笔趣阁也可以,经查看.学习一下
Reply View the author
avatar
老陌
deepin
2018-09-30 00:11
#2
https://bbs.deepin.org/post/169384
https://www.bequge.com笔趣阁也可以,经查看.学习一下

不错,哈哈。
Reply View the author
avatar
impressionyang
deepin
2018-09-30 00:23
#3
老陌可以去写GUI程序了呀233,加油
Reply View the author
avatar
老陌
deepin
2018-09-30 01:12
#4
https://bbs.deepin.org/post/169384
老陌可以去写GUI程序了呀233,加油

我不会写程序啊。
Reply View the author
avatar
rekols
deepin
2018-09-30 01:20
#5
还行还行
Reply View the author
avatar
xinyonghu
deepin
2018-09-30 06:09
#6
楼主,你开始沉迷网络小说了?难怪最近少见你发言
Reply View the author
avatar
老陌
deepin
2018-09-30 20:20
#7
https://bbs.deepin.org/post/169384
楼主,你开始沉迷网络小说了?难怪最近少见你发言

是啊,最近基本没有学习。
Reply View the author
avatar
180******66
deepin
2018-09-30 20:29
#8
老莫你好骚啊
Reply View the author
avatar
老陌
deepin
2018-09-30 20:48
#9

风骚吗?
Reply View the author