5
我有一個Scrapy中的網頁抓取工具,可以獲取數據項。我想異步地將它們插入到數據庫中。無阻塞Scrapy管道到數據庫
例如,我有一些插入項目到我的數據庫使用SQLAlchemy的核心事務:
def process_item(self, item, spider):
with self.connection.begin() as conn:
conn.execute(insert(table1).values(item['part1'])
conn.execute(insert(table2).values(item['part2'])
我的理解,它可能與扭曲的異步使用SQLAlchemy核心與alchimia
。下面是alchimia
的文檔代碼示例。
我不明白的是如何在alchimia框架中使用我的上述代碼。我如何設置process_item
使用反應堆?
我可以這樣做嗎?
@inlineCallbacks
def process_item(self, item, spider):
with self.connection.begin() as conn:
yield conn.execute(insert(table1).values(item['part1'])
yield conn.execute(insert(table2).values(item['part2'])
如何寫入反應器部件?
或者是否有一種更簡單的方法在Scrapy管道中執行非阻塞數據庫插入?
僅供參考,下面是alchimia
的文檔中的代碼示例:
from alchimia import TWISTED_STRATEGY
from sqlalchemy import (
create_engine, MetaData, Table, Column, Integer, String
)
from sqlalchemy.schema import CreateTable
from twisted.internet.defer import inlineCallbacks
from twisted.internet.task import react
@inlineCallbacks
def main(reactor):
engine = create_engine(
"sqlite://", reactor=reactor, strategy=TWISTED_STRATEGY
)
metadata = MetaData()
users = Table("users", metadata,
Column("id", Integer(), primary_key=True),
Column("name", String()),
)
# Create the table
yield engine.execute(CreateTable(users))
# Insert some users
yield engine.execute(users.insert().values(name="Jeremy Goodwin"))
yield engine.execute(users.insert().values(name="Natalie Hurley"))
yield engine.execute(users.insert().values(name="Dan Rydell"))
yield engine.execute(users.insert().values(name="Casey McCall"))
yield engine.execute(users.insert().values(name="Dana Whitaker"))
result = yield engine.execute(users.select(users.c.name.startswith("D")))
d_users = yield result.fetchall()
# Print out the users
for user in d_users:
print "Username: %s" % user[users.c.name]
if __name__ == "__main__":
react(main, [])