很多网站都有长篇连载或是分章节的小说可供在线阅读,但如果想要将所有章节下载下来并整理成一个格式良好的文本文件,则是很费功夫的。幸好可以用Python脚本来自动完成所有的工作。下面的两个脚本,将用来演示下载“当时明月”在新浪blog中连载的《明朝的那些事儿-历史应该可以写得好看》这一长篇历史小说。

第一个脚本时getlink.py,它的功能是获取各章节的链接。打开“当时明月”的blog,点击“我的所有文章”,作者所有发表的文章将被分页列出,其中标题形如“.?长篇.?明朝的那些事儿-历史应该可以写得好看\[(\d*)-(\d*)\]”都是该长篇的章节。getlink.py所要做的就是将这些章节及相应链接保存到links.dat中备用。
getlink.py
 1用Python编写自动下载网络小说的脚本#-*- coding: utf-8 -*-
 2用Python编写自动下载网络小说的脚本import urllib,re,os,sys,pickle
 3用Python编写自动下载网络小说的脚本from xml.dom.minidom import parse
 4用Python编写自动下载网络小说的脚本import xml.dom.minidom
 5用Python编写自动下载网络小说的脚本
 6用Python编写自动下载网络小说的脚本uid='1233526741' #当时明月的ID
 7用Python编写自动下载网络小说的脚本
 8用Python编写自动下载网络小说的脚本#读取章节及对应的链接
 9用Python编写自动下载网络小说的脚本chapters={} #存储章节及链接,如chapters['0001-0010']='/u/49861fd5010003ii'
10用Python编写自动下载网络小说的脚本for i in range(1,100):
11用Python编写自动下载网络小说的脚本    filehandle = urllib.urlopen('http://blog.sina.com.cn/sns/service.php?m=aList&uid=%s&sort_id=0&page=%d' % (uid,i))
12用Python编写自动下载网络小说的脚本    myDoc = parse(filehandle)
13用Python编写自动下载网络小说的脚本    myRss = myDoc.getElementsByTagName("rss")[0]
14用Python编写自动下载网络小说的脚本    items = myRss.getElementsByTagName("item")
15用Python编写自动下载网络小说的脚本    for item in items:
16用Python编写自动下载网络小说的脚本        title=item.getElementsByTagName("title")[0].childNodes[0].data
17用Python编写自动下载网络小说的脚本        link=item.getElementsByTagName("link")[0].childNodes[0].data
18用Python编写自动下载网络小说的脚本        match= re.search(ur'.?长篇.?明朝的那些事儿-历史应该可以写得好看\[(\d*)-(\d*)\]',title)
19用Python编写自动下载网络小说的脚本        if match:
20用Python编写自动下载网络小说的脚本            #print   match.group(1),":",match.group(2)
21用Python编写自动下载网络小说的脚本            chapters['%04d-%04d' % (int(match.group(1)),int(match.group(2)))]=item.getElementsByTagName("link")[0].childNodes[0].data
22用Python编写自动下载网络小说的脚本
23用Python编写自动下载网络小说的脚本# 将chapters保存到文件中以备用 
24用Python编写自动下载网络小说的脚本output = open('links.dat''wb+')
25用Python编写自动下载网络小说的脚本pickle.dump(chapters, output)
26用Python编写自动下载网络小说的脚本output.close()

当取得links.dat后,下面利用bookdownload.py将所有章节的内容下载下载并整理成最后的全文文件mingthings.txt。这一脚本的关键时第19行,从下载的内容中取得每一篇的实际内容(去除广告、脚本等)。通过分析每篇文章的html源文件发现,我们所要的东西都位于<div >  1用Python编写自动下载网络小说的脚本#-*- coding: utf-8 -*-
 2用Python编写自动下载网络小说的脚本import urllib,re,os,sys,pickle
 3用Python编写自动下载网络小说的脚本from xml.dom.minidom import parse
 4用Python编写自动下载网络小说的脚本import xml.dom.minidom
 5用Python编写自动下载网络小说的脚本
 6用Python编写自动下载网络小说的脚本uid='1233526741' #当时明月的ID
 7用Python编写自动下载网络小说的脚本
 8用Python编写自动下载网络小说的脚本#读取章节及对应的链接
 9用Python编写自动下载网络小说的脚本chapters={} #存储章节及链接,如chapters['0001-0010']='/u/49861fd5010003ii'
10用Python编写自动下载网络小说的脚本links = open('links.dat''rb+')#links.dat由getlinks.py生成
11用Python编写自动下载网络小说的脚本chapters=pickle.load(links)#从links.dat读取章节及对应链接信息到chapters
12用Python编写自动下载网络小说的脚本
13用Python编写自动下载网络小说的脚本book=open('mingthings.txt','w+'#mingthings.txt即为最终要生成的全文
14用Python编写自动下载网络小说的脚本for chapter in sorted(chapters):
15用Python编写自动下载网络小说的脚本    print chapter #输出当前正在处理的章节
16用Python编写自动下载网络小说的脚本    webpage=urllib.urlopen('http://blog.sina.com.cn'+chapters[chapter]).read().decode('utf-8')
17用Python编写自动下载网络小说的脚本
18用Python编写自动下载网络小说的脚本    # s: Dot match new line; i: Case insenstive; m: ^$ match at linebreaks 
19用Python编写自动下载网络小说的脚本    match=re.search(ur'(?siLu).*<div >book.close()

相关文章:

  • 2022-02-10
  • 2021-09-04
  • 2022-12-23
  • 2021-07-29
  • 2021-12-31
  • 2022-12-23
  • 2022-02-23
猜你喜欢
  • 2022-01-15
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
相关资源
相似解决方案