scrapy爬取小说以txt是形式存储,

一。 爬取的路径:

1.进入小说的目录页面https://www.x81zw.com/book/5/5182/
2.提取每个章节的连接
3.进入章节连接爬取文本内容和章节标题
4.将每个章节的内容进行保存

二。文件

1.spider.py

# -*- coding: utf-8 -*-
import scrapy
from novel.items import NovelItem
import re

class Demo1Spider(scrapy.Spider):
    name = 'demo1'#爬虫名字
    allowed_domains = ['x81zw.com']#爬取的域名
    start_urls = ['https://www.x81zw.com/book/5/5182/']#开始爬取的网址
    
    def parse(self, response):
    item = NovelItem()# NovelItem()类的对象实例化
    dds = response.xpath('//*[@id="list"]/dl/dd')#获取到目录中所有章节所在的位置
    for i in range(10, 100):#对前90章进行存取,因为dd[10]对应的是第一章,
    所以从10开始遍历
        href = "https://www.x81zw.com"+response.xpath('//*[@id="list"]/dl/dd[{}]/a/@href'.format(i))[0].extract()
        #因为提取的网址不全,所有需要对他进行补全,format函数实现i替换{}
        #[0]解决list问题,extract()是提取函数
        item['href'] = href
        #print(item['href'])
        yield scrapy.Request(url=item['href'], meta={"item": item},
                             callback=self.parse_detail, dont_filter=True)
          #这里是生成(yield)了一个request请求,请求的内容是url连接和meta数据,
          #meta数据保存着item数据的所有内容,通过callback传给parse_detail
          #进行下一步的解析,并且声明dont_filter=True,使得url不能重复


    def parse_detail(self, response):
        item = response.meta['item']
         #item接收由上面函数返回到这个函数请求数据中的meta['item'],类似于继承,
         #它既可以有上个函数属性(上个函数的item是QqnItem类的实例化),
         #又得到了它的所有数据
        contents = response.xpath('//*[@id="content"]/text()').extract()
        #对所有的文本内容进行提取,提取后是一个list表单
        item['title'] = response.xpath('//div[@class="bookname"]/h1/text()')[0].extract()
        #对标题进行提取
        item['content'] = ''
        #提取声明 item['content']这个变量
        for i in contents:
            # i = ''.join(i.split('\u3000\u3000'))
            item['content'] = item['content'] + "\n" + i.replace(u'\u3000', u''
            #对list里面的内容进行合并,并且把\u3000全部去掉
        print("最终的结果:")
        print(item['title'])
        # print(item['content'])
        yield item

2.item.py

import scrapy
class NovelItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    href = scrapy.Field()
    content = scrapy.Field()
    title = scrapy.Field()

3.piplines.py

class NovelPipeline(object):
  def process_item(self, item, spider):
    with open("星际之全能进化.txt", 'a', encoding='utf-8') as f:
        f.write(item['title'])
        f.write('\n')
        f.write('\n')
        f.write(item['content'])
        f.write('\n')
        f.write('\n')
        #这样存的目的是:标题和内容有一段间距,并且两个章节之间也要有间距

三。结果在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值