栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Scrapy中提供sqlite管道的示例代码吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Scrapy中提供sqlite管道的示例代码吗?

我做了这样的事情:

## Author: Jay Vaughan## Pipelines for processing items returned from a scrape.# Dont forget to add pipeline to the ITEM_PIPELINES setting# See: http://doc.scrapy.org/topics/item-pipeline.html#from scrapy import logfrom pysqlite2 import dbapi2 as sqlite# This pipeline takes the Item and stuffs it into scrapedata.dbclass scrapeDatasqLitePipeline(object):    def __init__(self):        # Possible we should be doing this in spider_open instead, but okay        self.connection = sqlite.connect('./scrapedata.db')        self.cursor = self.connection.cursor()        self.cursor.execute('CREATE TABLE IF NOT EXISTS myscrapedata '          '(id INTEGER PRIMARY KEY, url VARCHAr(80), desc VARCHAr(80))')    # Take the item and put it in database - do not allow duplicates    def process_item(self, item, spider):        self.cursor.execute("select * from myscrapedata where url=?", item['url'])        result = self.cursor.fetchone()        if result: log.msg("Item already in database: %s" % item, level=log.DEBUG)        else: self.cursor.execute(     "insert into myscrapedata (url, desc) values (?, ?)",         (item['url'][0], item['desc'][0]) self.connection.commit() log.msg("Item stored : " % item, level=log.DEBUG)        return item    def handle_error(self, e):        log.err(e)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373963.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号