首页 > 其他分享 >苦读书网站小说爬虫

苦读书网站小说爬虫

时间:2023-02-02 14:56:04浏览次数:33  
标签:章节 spider 网站 text 爬虫 replace item 读书 id

看小说消磨时间,但是广告太多了,且大多都是不健康的广告,
本着无聊练手的想法写了个小说爬虫,可以爬取小说并按小说名章节目录存储
网站是众多盗版网站中的一个
爬取效果如下

# xs_spider\xs_spider\spiders\sx_spider_01.py
import scrapy
from scrapy.http import Request
from xs_spider.items import XsSpiderItem
from pathlib import Path
from re import sub

class SxSpider01Spider(scrapy.Spider):
    name = 'sx_spider_01'
    allowed_domains = ['m.kudushu.net']
    start_urls = [
        'http://m.kudushu.net/book/999043/'
    ]

    base_url = 'http://m.kudushu.net'

    def parse(self, response):
        章节分页 = response.xpath('.//select[@name="pageselect"]//@value').getall()
        for i in 章节分页:
            yield Request(url=rf'{self.base_url}{i}',callback=self.parse2)

    def parse2(self,response):
        章节元素 = response.xpath('.//div[@class="info_menu1"]/div[@class="list_xm"]/ul/li/a')
        书名 = response.xpath('/html/body/div/div[4]/div[2]/h3/text()').get()
        for 章节 in 章节元素:
            章节链接 = self.base_url+章节.xpath('./@href').get()
            章节标题 = 章节.xpath('./text()').get()
            item = XsSpiderItem()
            章节id =[i for i in 章节链接.split(r'/') if i != ''][-1]
            小说id =[i for i in 章节链接.split(r'/') if i != ''][-2]
            item['小说id'] = 小说id + '_' + 书名
            item['章节id'] = 章节id
            item['标题'] = 章节标题
            item['链接'] = 章节链接

            文件路径 = Path(rf"./data/{item['小说id']}/{item['章节id']}_{item['标题']}.txt")
            if 文件路径.is_file():
                continue
            yield Request(url=章节链接,callback=self.parse3,meta={'item':item})

    def parse3(self,response):
        item = response.meta.get('item')
        texts = response.xpath('//*[@id="novelcontent"]//text()').getall()[1:]
        text = ','.join(texts).replace(u'\xa0','').replace(u'\n,','').replace(r',\n','')
        text = text.replace('(本章未完,请点击下一页继续阅读)','').replace(u'\r\n','')
        text = text.replace('。,','。')
        text = text.replace('最新网址:m.kudushu.net,','')
        text = text.replace('hedgeogbm();','')
        text = text.replace('&nbsp','')
        text = text.replace(',上—章','')
        text = text.replace('下—页','')
        text = text.replace('加入书签','')
        text = text.replace('上—页','')
        text = text.replace('返回目录','')
        text = text.replace(' ','')
        text = text.replace(',,',',')
        text = text.replace(',,',',')
        text = text.replace(',,',',')
        text = text.replace(',;,;,,','')
        text = text.replace(';,','')
        text = text.replace(';,','')
        text = text.replace(';,','')
        text = text.replace(';,','')
        text = sub(r'hedgeo.+?\)','',text)
        text = text.replace(',,',',')
        text = text.replace(',,',',')
        text = text.replace('\n\n\n',',')
        text = text.replace('\n\n',',')
        if '在手打中,请稍等片刻,内容更新后' in text:
            return

        if item.get('内容') is None:
            item['内容'] = text
        else:
            item['内容'] = item['内容']+'\n\n'+text

        if response.xpath('.//ul[@class="novelbutton"]/li/p[@class="p1 p3"]/a/text()').get() == '下—页':
            下一页链接 = response.xpath('.//ul[@class="novelbutton"]/li/p[@class="p1 p3"]/a//@href').get()

            yield Request(rf'{self.base_url}/{下一页链接}',meta={'item':item,},callback=self.parse3)
        else:
            yield item

# xs_spider\xs_spider\items.py

import scrapy


class XsSpiderItem(scrapy.Item):
    # define the fields for your item here like:
    标题 = scrapy.Field()
    链接 = scrapy.Field()
    内容 = scrapy.Field()
    章节id = scrapy.Field()
    小说id = scrapy.Field()

    # pass

# xs_spider\xs_spider\pipelines.py

from itemadapter import ItemAdapter
from pathlib import Path

class XsSpiderPipeline:
    def process_item(self, item, spider):
        文件路径 = rf"./data/{item['小说id']}/"
        文件路径 = Path(文件路径)
        if not 文件路径.is_dir():
            文件路径.mkdir(parents=True, exist_ok=True)

        with open(rf"./data/{item['小说id']}/{item['章节id']}_{item['标题']}.txt",'w',encoding='utf-8') as f:
            f.write(item['内容'])
        return item
# xs_spider\run.py
# 运行爬虫
from scrapy.cmdline import execute

execute("scrapy crawl sx_spider_01".split(' '))

标签:章节,spider,网站,text,爬虫,replace,item,读书,id
From: https://www.cnblogs.com/meizhengchao/p/17085958.html

相关文章

  • 益智拼图 - 值得把玩的“小网站”(2)
    本文是“小网站”系列的第2篇。本系列介绍一些作者主观认为值得把玩的“小网站”,愿朋友们多少都能有所收获。这个系列会长期更新,对这个系列感兴趣的朋友可关注,不迷路。......
  • 常用网站
    ColorSpace基于输入的颜色帮你生成完美的配色undraw免费开源插图网站sizzy面向前端开发者的浏览器,让你开发响应式设计站点变得更轻松......
  • 某某网站AES算法JS逆向分析(JS逆向第八期)
       时隔一周,我又回来更新了!本期继续分享JS逆向章节;今天分享的内容是某某网站的Response响应体加密,如果不够细心,甚至都不知道内容从哪里加载生成的。全程高能,在阅读的同......
  • python爬虫入门脚本示例
    例如在某个目录xx下,有个普通的文件,有些文件后缀*.txt,有的却没有后缀名。现在写一个程序希望将xx目录下没有后缀名的文件改成*.txt结尾。知识点:需要用到python的os模块直接......
  • 盗版电影网站原理
    欢迎关注我的公众号:夜说猫,让贫穷的程序员不靠打代码也能吃饭~作为一个IT软件工程师,不仅要会打代码,还要搞懂这些黑产。几年前,包括现在,视频网站各种VIP,VVIP,SUPERVIP,整得......
  • Kantana:下一代爬虫和爬虫框架
    介绍•快速且完全可配置的网络爬取•标准和无头模式支持•JavaScript解析/爬取•可定制的自动填表•范围控制-预配置字段/正则表达式•可定制的输出-预配置的字段•输入-......
  • 恒创科技:日本服务器搭建网站步骤教程
    随着出海的发展,让更多企业了解到了海外服务器的优势和好处,也使得服务器的选择不仅局限于国内,日本服务器近年来也开始进入越来越多企业和开发者的视线。与韩国服务器相似......
  • 浅谈爬虫开发中几种形式的cookie的互相转换与利用
    前言在我们写爬虫的过程中,cookie一般是我们最经常接触到的东西。而由于在爬虫过程中的各个阶段的难度往往不同,所以我们很多时候会采用浏览器、requests等等各种方案来在采......
  • 爬虫经验
    示例网站:  1、点页码只有后一部分start=1改变  2、点步长只有第一次url改变  构造方法 :尝试在1后加上2的一部分 ......
  • 客服系统即时通讯IM开发(六)Glang Gorm 执行原生Sql语句增删改查封装库【唯一客服】网站
    在开发在线客服系统的时候,有某些地方需要使用脚本去批量执行SQL语句,这个时候就需要使用简单的执行SQL的封装函数了查询操作是使用的原生的sql库,没用Gorm,原因是Gorm的RawS......