笔趣阁全站小说爬取

前言:代码百分百手打,无抄袭
整理一下思路
1.向目标网站发起请求,获取书名,章节,内容
2.拿到内容后清洗
3.将清洗后的数据存入txt文件

用到的库:requests,re目标网站新笔趣阁

获取每一本书的url

在这里插入图片描述
点击全部小说
在这里插入图片描述
查看源代码寻找书籍url,名称
在这里插入图片描述

l进入小说详细页

在这里插入图片描述
查看页面源代码,寻找章节url
在这里插入图片描述

获取小说详细内容

在这里插入图片描述
在这里插入图片描述

然后清洗,写入txt文件

代码贴上,实测可用人头担保🤔

import requests
import re
import time
all_info=''
url='http://www.xbiquge.la/xiaoshuodaquan/'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 Edg/84.0.522.40'}
html=requests.get(url=url,headers=headers)
html=html.text
books_info=re.findall('<div class="novellist">(.*?)</ul></div>',html,re.S)
for book_info in books_info:
    book_info=re.findall('<li><a href="(.*?)">(.*?)</a></li>',book_info,re.S)
    for book_url,book_name in book_info:
        html=requests.get(url=book_url,headers=headers)
        html.encoding='utf-8'
        html=html.text
        chapter=re.findall('<div id="list">(.*?)</dl>',html,re.S)
        for chapter_list in chapter:
            chapter_info=re.findall("<dd><a href='(.*?)' >(.*?)</a></dd>",chapter_list,re.S)
            for chapter_url,chapter_name in chapter_info:
                chapter_url='http://www.xbiquge.la/{}'.format(chapter_url)
                html=requests.get(url=chapter_url,headers=headers)
                html.encoding='utf-8'
                html=html.text
                infos=re.findall('<div id="content">(.*?)</div>',html)
                info=str(infos)
                
                info=info.replace('&nbsp;','')
                info=info.replace(r'\r<br />','')
                info=info.replace(r'<br />','')
                info=info.replace(r'&amp;ldquo;','')
                info=info.replace('amp;','')
                info=info.replace('ldquo;','')
                info=info.replace('rdquo;','')
                info=info.replace('gt;','')
                info=info.replace(r'/p&amp;','')
                info=info.replace('</p>','')
                info=info.replace(r'&/p&','')
                info=info.replace(r'<p>','')
                info=info.replace(r'</a>','')
                print('='*40)
                print('正在下载'+book_name+chapter_name)
                print('='*40)
                f=open(book_name+'.txt','a',encoding='utf-8')
                f.write(info)
                f.close()
                time.sleep(2)
            

效果图,再也不用担心闹书荒😝

在这里插入图片描述
在这里插入图片描述
由于未使用多进程,速度有点慢,别在意,下篇出个多进程…(* ̄0 ̄)ノ

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值