当屌丝想看《蜀山剑侠传》[python屏幕抓取]

想看《蜀山》,就下载了蜀山剑侠传.txt.放在的电子书里。

但看了会觉得文件挺大的,电子书加载起来也挺慢了,也没下着分回目版的,就想着把它按章回拆分到的各个文件。

想想无非就是读取下文件,正则匹配一下,文件分割下,就完事大吉了。 

coding时觉得这种方式肯定慢, 不如去在线阅读的地方抓取一下。于是找到【蜀山剑侠传---还珠楼主---天涯在线书库】,把文件分割的问题变成屏幕抓取的问题。

code:

from urllib import urlopen
import re

titleRe = re.compile('(?<="biaoti">).+?(?=</span>)')
contentRe = re.compile("(?<='content'>).+?(?=</td>)",re.DOTALL)

dirPath = 'f:\shushanjianxiazhuan\\'
urlPath = 'http://www.tianyabook.com/wuxia/huanzhulouzhu/shushanjianxiazhuan/'

for x in xrange(1,310):
    x = str(x)
    url = urlPath + x+ '.htm'
    page = urlopen(url).read()
    title = titleRe.search(page).group()
    content = contentRe.search(page).group()
    content = content.replace('<BR>','\n')
    f = file(dirPath+x+title+'.txt','w')
    f.write(title+'\n'+content)
    f.close()
    print title

子在川上曰:《蜀山》是一部超级超级超级浪漫恢宏的作品,只可惜我早生了两千年。

原文地址:https://www.cnblogs.com/flowerszhong/p/2742615.html