Scrapy-02


  • item管道:

    • scrapy提供了item对象来对爬取的数据进行保存,它的使用方法和字典类似,不过,相比字典,item多了额外的保护机制,可以避免拼写错误和定义字段错误。
    • 创建的item需要继承scrapy.Item类,并且在里面定义Field字段。(我们爬取的是盗墓笔记,只有文章标题和内容两个字段)
    • 定义item,在item.py中修改:
    •  # -*- coding: utf-8 -*-
      
       # Define here the models for your scraped items
      #
      # See documentation in:
      # https://doc.scrapy.org/en/latest/topics/items.html import scrapy class BooksItem(scrapy.Item):
      # define the fields for your item here like:
      # name = scrapy.Field()
      title = scrapy.Field()
      content = scrapy.Field()
    • 解析response和对item的使用:
       # -*- coding: utf-8 -*-
      import scrapy
      from ..items import BooksItem class DmbjSpider(scrapy.Spider):
      name = 'dmbj'
      allowed_domains = ['www.cread.com']
      start_urls = ['http://www.cread.com/chapter/811400395/69162457.html/'] def parse(self, response):
      item = BooksItem()
      item['title'] = response.xpath('//h1/text()').extract_first()
      item['content'] = response.xpath('//div[@class="chapter_con"]/text()').extract_first()
      yield item
       # -*- coding: utf-8 -*-
      
       # Define your item pipelines here
      #
      # Don't forget to add your pipeline to the ITEM_PIPELINES setting
      # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html class BooksPipeline(object):
      def process_item(self, item, spider):
      with open('files/{}.txt'.format(item['title']), 'w+') as f:
      f.write(item['content'])
      return item def open_spider(self, spider):
      # 爬虫启动时调用
      pass def close_spider(self, spider):
      # 爬虫关闭时调用
      pass

      在parse方法中导入item中定义需要的类,将该类实例化,实例化的类对他进行字典的方式操作,直接对其赋值,字典的key值必须和类中对应的字段名字一直。

    • 然后对其使用yield
    • 在pipline.py里面定义三个方法:
      • process_item:

        • 对parse返回的item进行处理,然后在返回出去
      • open_spider:
        • 爬虫启动的时候自动调用
      • close_spider:
        • 爬虫关闭的时候调用
    • pipline里面定义的pipline需要使用,就得到setting里面讲ITEM_PIPELINES的字典激活
    • ITEM_PIPELINES = {
      'books.pipelines.BooksPipeline': 300,
      }
  • shell
    • scrapy  shell 是scrapy提供的一个交互式的调试工具,如果当前环境中安装了ipython,那么将默认调用ipython,也可以在scrapy.cfg的setting下设置: shell = ipython
    • 使用scrapy  shell:
      • 终端输入: scrapy shell   [url]          //url:想爬取的网址,可不添加(也可以是个本地的文件,以路径的方式写入)
    • fetch:
      • fetch接受一个url,构成一个新的请求对象,对返回新的response

最新文章

  1. MySQL索引原理及慢查询优化
  2. codevs1501 二叉树最大宽度和高度
  3. 面向服务体系架构(SOA)和数据仓库(DW)的思考基于 IBM 产品体系搭建基于 SOA 和 DW 的企业基础架构平台
  4. Scrum 项目 6.0
  5. C++学习之重载运算符1
  6. QtSoap调用Web Service(QtSoap是非官方应用)
  7. Codeforces Round #253 (Div. 1) B. Andrey and Problem
  8. 【BJG吐槽汇】第2期 - 我每周1-2次迟到都是因为你-->ios10!
  9. Openlayers 3 的 imagelayer
  10. 初探CSRF在ASP.NET Core中的处理方式
  11. AIX smit下创建逻辑卷、添加文件系统并挂载
  12. [转]react 部署在ngnix上(windows环境)
  13. 使用DDL触发器同步多个数据库结构
  14. [APM] 解读2016之APM国内篇:快速增长的APM市场和技术
  15. app测试初窥
  16. head first python 支持网站(可下载所有的代码和示例)
  17. iOS中UIView翻转效果实现
  18. Vim:replace with foobar (y/n/a/q/l/^E/^Y)?
  19. 左右值无限级分类 MVC + EntityFramework 的简单实现
  20. JDBC处理大数据

热门文章

  1. [android]com.android.support:appcompat-v7:XXX 包导入无法Build
  2. Python基础篇_实例练习(二)
  3. 【股票盯盘软件】01_程序员炒股之开发一款极简风格的股票盯盘软件StockDog_V1.0.0.1
  4. 超实用的Flask入门基础教程,新手必备!
  5. 记录一次云主机部署openstack的血泪史
  6. IE 跨域session丢失问题
  7. WEB应用之httpd基础入门(三)
  8. Spring Controller单例与线程安全那些事儿
  9. JRebel 破解使用
  10. Array.forEach原理,仿造一个类似功能