Scrapy实战案例

前言:
网上多案例链接都无效,本篇为有效案例(如果链接失效,请留言笔者)笔者将第一时间更新。本篇非入门案例,如果想看入门案例,请看笔者的scray学习一二三的案例
(talk is cheap,show you code right now)

项目结构

该爬虫作用是从网站爬取《百年孤独》这个长篇小说
在这里插入图片描述
xpathtest.py内容

import scrapy
from xpathtest.items import XpathtestItem
class XpathTest(scrapy.Spider):
    name = "xpathtest"
    start_urls = ['https://www.luoxia.com/bainiangudu/']
    allowed_domains = ['luoxia.com']
    def parse(self, response):
        aa = response.xpath('//div[@class="book-list clearfix"]/ul/li/a/@href').extract()
        for url in aa:
            yield scrapy.Request(url, callback=self.parse_dir_contents)

    def parse_dir_contents(self,response):
        item = XpathtestItem()
        item["name"] = response.css('title::text').extract()[0] #提取标题
        item["content"] = response.xpath('//div[@id="nr1"]/p/text()').extract() #提取文章内容

        yield item

piplines.py内容

# -*- coding: utf-8 -*-

# 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


class XpathtestPipeline(object):
    def process_item(self, item, spider):
        filename ="c:/test/"+item["name"]+".txt"
        with open(filename,'w') as f:
           for i in item["content"]:
               f.write(i+"\n")
        return item

settings.py里面添加内容

ITEM_PIPELINES = {
   'xpathtest.pipelines.XpathtestPipeline': 300,
}

DOWNLOAD_DELAY = 2 #抓取延迟,防止出现page not available报错 

items.py内容

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


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

最后
在c盘先新建文件夹test
在项目所在文件夹打开cmd命令界面
输入scrapy crawl xpathtest
爬取内容展示
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值