【百度云搜索,搜各种资料:http://www.bdyss.cn】

【搜网盘,搜各种资料:http://www.swpan.cn】

注意:数据保存的操作都是在pipelines.py文件里操作的

将数据保存为json文件

spider是一个信号检测

# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.htmlfrom scrapy.pipelines.images import ImagesPipeline  #导入图片下载器模块import codecsimport jsonclass AdcPipeline(object):                      #定义数据处理类,必须继承object    def __init__(self):        self.file = codecs.open('shuju.json', 'w', encoding='utf-8')  #初始化时打开json文件    def process_item(self, item, spider):       #process_item(item)为数据处理函数,接收一个item,item里就是爬虫最后yield item 来的数据对象        # print('文章标题是:' + item['title'][0])        # print('文章缩略图url是:' + item['img'][0])        # print('文章缩略图保存路径是:' + item['img_tplj'])  #接收图片下载器填充的,图片下载后的路径        #将数据保存为json文件        lines = json.dumps(dict(item), ensure_ascii=False) + '\n'   #将数据对象转换成json格式        self.file.write(lines)          #将json格式数据写入文件        return itemdef spider_closed(self,spider):     #创建一个方法继承spider,spider是一个信号,当前数据操作完成后触发这个方法        self.file.close()               #关闭打开文件class imgPipeline(ImagesPipeline):                      #自定义一个图片下载内,继承crapy内置的ImagesPipeline图片下载器类    def item_completed(self, results, item, info):      #使用ImagesPipeline类里的item_completed()方法获取到图片下载后的保存路径        for ok, value in results:            img_lj = value['path']     #接收图片保存路径            # print(ok)            item['img_tplj'] = img_lj  #将图片保存路径填充到items.py里的字段里        return item                    #将item给items.py 文件的容器函数    #注意:自定义图片下载器设置好后,需要在

将数据保存到数据库

我们使用一个ORM框架sqlalchemy模块,保存数据

数据库操作文件

#!/usr/bin/env python# -*- coding:utf-8 -*-from sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy import Columnfrom sqlalchemy import Integer, String, TIMESTAMPfrom sqlalchemy import ForeignKey, UniqueConstraint, Indexfrom sqlalchemy.orm import sessionmaker, relationshipfrom sqlalchemy import create_engine#配置数据库引擎信息ENGINE = create_engine("mysql+pymysql://root:279819@127.0.0.1:3306/cshi?charset=utf8", max_overflow=10, echo=True)Base = declarative_base()       #创建一个SQLORM基类class SendMsg(Base):            #设计表    __tablename__ = 'sendmsg'    id = Column(Integer, primary_key=True, autoincrement=True)    title = Column(String(300))    img_tplj = Column(String(300))def init_db():    Base.metadata.create_all(ENGINE)        #向数据库创建指定表def drop_db():    Base.metadata.drop_all(ENGINE)          #向数据库删除指定表def session():    cls = sessionmaker(bind=ENGINE)         #创建sessionmaker类,操作表    return cls()# drop_db()         #删除表# init_db()         #创建表

pipelines.py文件

# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.htmlfrom scrapy.pipelines.images import ImagesPipeline  #导入图片下载器模块from adc import shujuku as ORM                      #导入数据库文件class AdcPipeline(object):                      #定义数据处理类,必须继承object    def __init__(self):        ORM.init_db()                           #创建数据库表    def process_item(self, item, spider):       #process_item(item)为数据处理函数,接收一个item,item里就是爬虫最后yield item 来的数据对象        print('文章标题是:' + item['title'][0])        print('文章缩略图url是:' + item['img'][0])        print('文章缩略图保存路径是:' + item['img_tplj'])  #接收图片下载器填充的,图片下载后的路径        mysq = ORM.session()        shuju = ORM.SendMsg(title=item['title'][0], img_tplj=item['img_tplj'])        mysq.add(shuju)        mysq.commit()        return itemclass imgPipeline(ImagesPipeline):                      #自定义一个图片下载内,继承crapy内置的ImagesPipeline图片下载器类    def item_completed(self, results, item, info):      #使用ImagesPipeline类里的item_completed()方法获取到图片下载后的保存路径        for ok, value in results:            img_lj = value['path']     #接收图片保存路径            # print(ok)            item['img_tplj'] = img_lj  #将图片保存路径填充到items.py里的字段里        return item                    #将item给items.py 文件的容器函数    #注意:自定义图片下载器设置好后,需要在

【转载自:http://www.lqkweb.com】