将项保存到sql数据库的scrapy扩展

scrapy-sqlitem的Python项目详细描述


scrapy sqlitem

scrapy sqlitem允许您使用sqlalchemy模型定义scrapy项 或者桌子。它还提供了一种在 大块的。

这个项目是测试版的。欢迎提出请求和反馈。这个 使用SQL数据库后端进行重载写的常规注意事项 应用程序仍然适用。

灵感来自 scrapy-redisscrapy-djangoitem

快速启动

pip install scrapy_sqlitem

Define items using Sqlalchemy ORM

fromscrapy_sqlitemimportSqlItemclassMyModel(Base):__tablename__='mytable'id=Column(Integer,primary_key=True)name=Column(String)classMyItem(SqlItem):sqlmodel=MyModel

Or Define Items using Sqlalchemy Core

fromscrapy_sqlitemimportSqlItemclassMyItem(SqlItem):sqlmodel=Table('mytable',metadataColumn('id',Integer,primary_key=True),Column('name',String,nullable=False))

如果尚未创建表,请确保创建它们。见 sqlalchemy文档和示例spider。

使用sqlspider可以很容易地将刮下的项保存到数据库

settings.py

DATABASE_URI="sqlite:///"

定义蜘蛛

fromscrapy_sqlitemimportSqlSpiderclassMySpider(SqlSpider):name='myspider'start_urls=('http://dmoz.org',)defparse(self,response):selector=Selector(response)item=MyItem()item['name']=selector.xpath('//title[1]/text()').extract_first()yielditem

运行蜘蛛

scrapy crawl myspider

查询数据库

Select*frommytable;id|name|----+-----------------------------------+
1|DMOZ-theOpenDirectoryProject|

其他信息

不想使用sqlspider?改为编写管道。

fromsqlalchemyimportcreate_engineclassCommitSqlPipeline(object):def__init__(self):self.engine=create_engine("sqlite:///")defprocess_item(self,item,spider):item.commit_item(engine=self.engine)

在保存到数据库之前删除缺少所需主键数据的项

fromscrapy.exceptionsimportDropItemclassDropMissingDataPipeline(object):defprocess_item(self,item,spider):ifitem.null_required_fields:raiseDropItemelse:returnitem# Watch out for Serial primary keys that are considered null.

以块而不是逐项保存到数据库

继承自sqlspider和..

在“设置”中

DEFAULT_CHUNKSIZE=500CHUNKSIZE_BY_TABLE={'mytable':1000,'othertable':250}

如果将块保存到数据库时出错,它将尝试保存 每个项目一次一个

访问底层的sqlalchemy表以查询数据库

INSERTINTOmytable(id,name)VALUES('1','ryan')
myitem=MyItem()# bind the table to an engine (I could have done this when I created the table too)myitem.table.metadata.bind=self.enginemyitem.table.select().where(item.table.c.id==1).execute().fetchone()(1,'ryan')

数据库中的哪一行与我的项中的数据匹配?

myitem=MyItem()myitem['id']=1myitem.get_matching_dbrow(bind=self.engine)(1,'ryan')

这和上面的查询是一样的!

有问题

如果您对刮伤的项或蜘蛛关闭的项进行子类划分,请确保调用super!

classMySpider(SqlSpider):defparse(self,response):passdefspider_closed(self,spider,reason):super(MySpider,self).spider_closed(spider,reason)self.log("Log some really important custom stats")
请小心其他混合剂。继承结构可以得到一些 凌乱。如果mro子类项中的一个类被刮擦而没有 call super sqlspider的item_scraped方法永远不会被调用。

sqlitem的其他方法

sqlitem.table

  • 返回与该项对应的sqlalchemy核心表。

sqlitem.null必需字段

  • 返回一组标记为NOT的数据库键名 可为空,项中的相应数据为空。

sqlitem.null主关键字字段

  • 返回一组主键名,其中 在项中为空。

sqlitem.主键

sqlitem.必需的密钥

sqlitem.get_matching_dbrow(bind=none,use_cache=true)

  • 在数据库中查找与中的主键数据匹配的数据 项目

待办事项

  • 连续积分测试

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
为@Nonnull注释参数编写java单元测试   对于JAVA,如何从它自己的类调用插入排序来将数组分类到一个单独的类中?   Java中嵌套SQL查询的字符串解析   java在所有带有特定注释的方法上调用带注释的方面   在Java中将base64转换为PDF   无法在Android上强制转换java getApplicationContext()   雅加达ee Java ee:如何获取我的应用程序的URL?   IntelliJ:如何导入的文件夹。java文件作为库?   多线程Java(FX)在播放一种声音的同时,播放另一种声音   网络爬虫我需要将proto3版本中protobuf生成的java代码添加到nutch 1.7中   使用JAXRS注释资源的java Restlet客户端   java如何查找给定月份的日期范围   java Minecraft Bukkit插件:我的小游戏传送机不工作   Java Excel搜索和数组列表工作不正常