使用scrapy爬取平凡的世界

import scrapy
from ..items import PfdsjItem


class PfsjSpider(scrapy.Spider):
    name = 'pfsj'
    #allowed_domains = ['xxx.com']
    start_urls = ['https://www.pingfandeshijie.net']

    #获取一级页面中的a链接地址
    def parse(self,response):
        #获取目录链接
        a_href=response.xpath("//center/table/tr/td/center/h2/a/@href").extract()
        # print(a_href)
        for v in a_href:
            # print(v)
            # 将返回的a链接交给调度器进行处理,将处理的结果传递给two_parse
            yield scrapy.Request(url=v,callback=self.two_parse)

    # 获取二级页面中的a链接地址
    def two_parse(self,respond):
        # print(respond)
        # 获取a链接
        a_href=respond.xpath('//div[@class="main"]/div[2]/ul/li/a/@href').extract()
        # print(a_href)
        for i in a_href:
            # 将返回的a链接交给调度器进行处理,将处理的结果传递给three_parse
            yield scrapy.Request(url=i,callback=self.three_parse)

    # 获取三级页面中的a链接地址
    def three_parse(self,respond):
        # print(type(book_name))
        page=respond.xpath('/html/body/div[3]/h1/text()').get().split()
        part=page[0]
        if len(page)>1:
            page_num=page[1]
        else:
            page_num = page[0]
        content=respond.xpath('//body/div[3]/div[2]/p/text()').extract()
        content='\n'.join(content)
        # print(content)
        item = PfdsjItem()
        # 给KugouItem对象属性赋值
        item['page_num'] = page_num
        item['part'] = part
        item['content'] = content.replace('\\u300', '')
        yield item
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import os
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class PfdsjPipeline:
    # 第一次执行管道类文件的时候执行一次
    def open_spider(self,spider):
        dirname = './平凡的世界'
        if not os.path.exists(dirname):
            os.mkdir(dirname)

    def process_item(self, item, spider):
        dirname = './%s/'%('平凡的世界') + item['part']
        if not os.path.exists(dirname):
            os.mkdir(dirname)
        # 章节名/章节数——标题
        filename = "./%s/%s/%s" % ('平凡的世界',item['part'],item['page_num'])
        with open(filename + '.txt', 'a', encoding='utf-8') as f:
            f.write(item['content'])
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class PfdsjItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    book_name = scrapy.Field()
    part = scrapy.Field()
    page_num = scrapy.Field()
    content=scrapy.Field()

所需第三方库:scrapy库

运行结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值