吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 20883|回复: 30
收起左侧

[Python 转载] 笔趣阁小说下载

[复制链接]
null119 发表于 2018-4-18 18:19
本帖最后由 null119 于 2018-5-4 23:01 编辑

话说论坛有不少关于笔趣阁小说网站的贴子了,今天又看到一个,正好无事,用Python写个简单的指定页面小说下载

俗话说无图无真像,先上图:

QQ截图20180418181544.png

再上代码:
[Python] 纯文本查看 复制代码
#coding:utf8
from multiprocessing.dummy import Pool as ThreadPool
import multiprocessing
import requests,os,codecs,time
from lxml import etree

url = 'https://www.biquge5200.com/3_3331/' #要下载的小说章节列表页面url

def getsource(url):
        try:
                s = requests.get(url)
        except:
                print('访问异常,跳过~!')
        else:
                s.encoding = 'gbk'
                return s.text

def getlist(url):
                global txtname,txtzz
                html = getsource(url)
                ehtml = etree.HTML(html)
                u = ehtml.xpath('//*[@id="list"]/dl/dd/a/@href')
                t = ehtml.xpath('//*[@id="list"]/dl/dd/a/text()')
                txtname = ehtml.xpath('//*[@id="info"]/h1/text()')[0].replace('\\','').replace('/','').replace(':','').replace('*','').replace('?','').replace('"','').replace('<','').replace('>','').replace('|','')
                txtzz = ehtml.xpath('//*[@id="info"]/p[1]/text()')[0].replace('\xa0','')
                num = 0
                for i in range(9,len(u)):
                        urllist.append(u[i]+'|'+t[i]+'|'+str(num))
                        num+=1

def downtxt(url):
        global downcount
        u = url.split('|')[0]
        t = url.split('|')[1]
        num = url.split('|')[2]
        content = ''
        while len(content) == 0:
                html = getsource(u)
                ehtml = etree.HTML(html)
                content = ehtml.xpath('string(//*[@id="content"])').replace('    ','\r\n').replace('  ','\r\n').replace('\xa0','').replace('\ufffd','').replace('\u266a','').replace('readx;','')
        if os.path.exists(savepath+num+'.txt'):
                print(num+'.txt 已经存在!')
        else:
                with codecs.open(savepath+num+'.txt', 'a')as f:
                                f.write('\r\n'+t +'\r\n'+ content)    
                print(t +' 下载完成!')
                downcount+=1

time_start = time.time();
downcount = 0
urllist = []
getlist(url)
savepath = os.getcwd()+'\\'+txtname+'\\'
if os.path.exists(savepath)== False:
        os.makedirs(savepath)
pool = ThreadPool(multiprocessing.cpu_count())
results = pool.map(downtxt,urllist)
pool.close()
pool.join()
print('开始合并txt...')
with codecs.open(savepath+txtname+'.txt', 'a')as f:
        f.write(txtname)
        f.write('\r\n')
        f.write(txtzz)
        f.write('\r\n')
        for i in range(0,len(urllist)):
                with open(savepath+str(i)+'.txt', "r") as fr:
                        txt = fr.read()
                        f.write(txt)
                        f.write('===========================')
                        fr.close()
                        os.remove(savepath+str(i)+'.txt')
print('小说合并完成~!')

print('')
print('*'*15 + ' 任务完成,结果如下:'+'*'*15)
print('')
print('<'+txtname+'> 下载完成'+',获取并下载章节页面:'+str(downcount)+' 个')
print('')
print('耗时:'+ str(time.time()-time_start)+ ' s')
print('')
print('*'*51)



最后,没有了

免费评分

参与人数 13吾爱币 +16 热心值 +13 收起 理由
moril + 1 + 1 很棒的!!!
testsoso + 1 + 1 我很赞同!
xoradd + 1 + 1 谢谢@Thanks!
heroabo + 1 + 1 热心回复!
fhyz + 1 + 1 谢谢@Thanks!
lai2000 + 1 + 1 谢谢@Thanks!
夏橙M兮 + 1 + 1 谢谢@Thanks!
zjkwwr + 1 + 1 python,2会报错,给力
画心233 + 1 用心讨论,共获提升!
苏紫方璇 + 5 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
fanvalen + 1 + 1 我很赞同
道本 + 1 + 1 热心回复!
745237896 + 1 + 1 东西呢

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| null119 发表于 2018-4-19 22:26
夏橙M兮 发表于 2018-4-19 13:01
楼主,感谢分享。不错,我试了下。为什么不是按顺序的呀?

你可以打开合并后的小说看下,顺序有没有错乱,至于你说的为什么没有按顺序下载,注意代码中的以下两句:
pool = ThreadPool(multiprocessing.cpu_count())
results = pool.map(downtxt,urllist)
mosou 发表于 2018-4-25 20:35
莫小应 发表于 2018-4-25 18:24
等你学了php就不会去想着学python了,至少很长一段时间是不会的

哈哈  C基础都有的  看什么语法都差不多
mosou 发表于 2018-4-18 18:39
给你点个赞。。。PY还得我学完php后下一个的目标
我是胡戈 发表于 2018-4-18 19:24
楼主威武
thyonezhy 发表于 2018-4-18 19:26
感谢分享
HU129131 发表于 2018-4-18 20:05
感谢分享  长学问了
cns拉斯 发表于 2018-4-18 22:02 来自手机
感谢分享。
ldqiang 发表于 2018-4-19 06:47
纯粹不会,白瞎楼主的好意了,谢谢了。先让我哭会儿。
君月栩 发表于 2018-4-19 08:21
楼主,有什么好的学习爬虫的方法吗?我入了一点门后,一直没进步了

点评

多写,试着举一反三  详情 回复 发表于 2018-4-19 08:38
 楼主| null119 发表于 2018-4-19 08:38
君月栩 发表于 2018-4-19 08:21
楼主,有什么好的学习爬虫的方法吗?我入了一点门后,一直没进步了

多写,试着举一反三
wushaominkk 发表于 2018-4-19 08:56
欢迎大佬经常来编程区发布文章
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 23:15

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表