Python带进度条的小说爬虫(笔趣网爬小说—简约版)

     Python带进度条的小说爬虫(笔趣网爬小说—简约版)

准备:

       BeautifulSoup、selenium、PhantomJS 注意PhantomJS安装方式

这里附上安装方式连接:

     https://blog.csdn.net/qq_42543301/article/details/81542880

其他的自行百度 或者pip install一下

还有一点需要注意:

   有可能selenium的版本过高带来的问题

   解决连接:https://www.cnblogs.com/zouke1220/p/9379839.html

开始:(没啥可以说的,很简单,注释写的很清楚)

#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#-----------------------------------以上为控制编码方式
from bs4 import BeautifulSoup
from selenium import webdriver
#-----------------------------------以上为引入所需要的包
target='https://www.biqukan.com'   #网站链接
url='https://www.biqukan.com/1_1094/5403177.html' #小说网内网址
index=1   #全局变量下载基数

#-------------下载函数----------------
def download (nexturl):
    global index  #全局变量声明一下
    print nexturl #调试输出一下网址
    driver = webdriver.PhantomJS() #加载PhantomJS 如果在ide里一直报不支持的错误并且也按照帖子安装好了,那就重启试一下
    driver.get(nexturl)
    data = driver.page_source
    driver.quit()
    source = BeautifulSoup(data,"lxml")   #解析一下执行过js的网页代码
    res=source.find(id="content",class_="showtxt").get_text() #找到存放小说内容的标签
    title = source.select(".content h1")[0].get_text()      #获取小说的章节标题
    #------------写入文件------------------
    with open(title + ".txt", "w") as f:
        for ind in range(len(res)):
            i = int(float(ind+1)/len(res)*100)
            s1 = "\r[%s%s]%d%%" % ("*" * i, " " * (100 - i), i)
            sys.stdout.write(s1)
            sys.stdout.flush()             #清空缓冲区
            time.sleep(0.001)
        f.close()
        index =index + 1
        print title+"下载完成!"

    #获取下一章的连接
    next = source.find(class_="page_chapter")
    next = next.select("li a")
    if len(next)<4 or index>10: #4的含义是存放上一章下一章的div里共4个li 若少于4个说明下一章不在了即最后一章  10 是表示最多下载10章
        print "下载结束!"
        return
    else:
        for a in next:
            if a.get_text()=="下一章":
                download(target+a.get("href"))   #简单递归调用
if __name__=="__main__":
    download(url)


望观看大神指教12

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值