Scrapy で SQLite パイプラインのサンプル コードを探しています。それに対するサポートが組み込まれていないことは知っていますが、それが行われていることは確かです。非常に限られたタスクを完了するのに十分なPythonとScrapyしか知らず、出発点としてコードが必要なので、実際のコードだけが私を助けることができます.
13079 次
5 に答える
16
私はこのようなことをしました:
#
# 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 log
from pysqlite2 import dbapi2 as sqlite
# This pipeline takes the Item and stuffs it into scrapedata.db
class 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)
于 2012-01-04T20:56:42.313 に答える
3
twisted の adbapi に慣れている場合は、次の mysql パイプラインを開始点として使用できます: http://github.com/darkrho/scrapy-googledir-mysql/blob/master/googledir/pipelines.py
そして、次の行を使用します__init__
。
self.dbpool = adbapi.ConnectionPool("sqlite3", database="/path/sqlite.db")
于 2010-07-20T21:13:05.267 に答える
2
同様の問題を解決しようとしている人のために、SQLite 用の素敵な Sqlite Item Exproter に出会いました: https://github.com/RockyZ/Scrapy-sqlite-item-exporter。
プロジェクト設定に含めた後、次のように使用できます。
scrapy crawl <spider name> -o sqlite.db -t sqlite
また、Item Exporter の代わりに Item Pipeline として使用するように適合させることもできます。
于 2015-06-01T22:46:10.433 に答える