Scrapy-02-item管道、shell、选择器
2024-09-07 00:42:26
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:
- 爬虫关闭的时候调用
- process_item:
- 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
最新文章
- MySQL索引原理及慢查询优化
- codevs1501 二叉树最大宽度和高度
- 面向服务体系架构(SOA)和数据仓库(DW)的思考基于 IBM 产品体系搭建基于 SOA 和 DW 的企业基础架构平台
- Scrum 项目 6.0
- C++学习之重载运算符1
- QtSoap调用Web Service(QtSoap是非官方应用)
- Codeforces Round #253 (Div. 1) B. Andrey and Problem
- 【BJG吐槽汇】第2期 - 我每周1-2次迟到都是因为你-->;ios10!
- Openlayers 3 的 imagelayer
- 初探CSRF在ASP.NET Core中的处理方式
- AIX smit下创建逻辑卷、添加文件系统并挂载
- [转]react 部署在ngnix上(windows环境)
- 使用DDL触发器同步多个数据库结构
- [APM] 解读2016之APM国内篇:快速增长的APM市场和技术
- app测试初窥
- head first python 支持网站(可下载所有的代码和示例)
- iOS中UIView翻转效果实现
- Vim:replace with foobar (y/n/a/q/l/^E/^Y)?
- 左右值无限级分类 MVC + EntityFramework 的简单实现
- JDBC处理大数据
热门文章
- [android]com.android.support:appcompat-v7:XXX 包导入无法Build
- Python基础篇_实例练习(二)
- 【股票盯盘软件】01_程序员炒股之开发一款极简风格的股票盯盘软件StockDog_V1.0.0.1
- 超实用的Flask入门基础教程,新手必备!
- 记录一次云主机部署openstack的血泪史
- IE 跨域session丢失问题
- WEB应用之httpd基础入门(三)
- Spring Controller单例与线程安全那些事儿
- JRebel 破解使用
- Array.forEach原理,仿造一个类似功能