gpt4 book ai didi

python - 如何将参数传递给 scrapy 管道对象

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:44 25 4
gpt4 key购买 nike

用 Scrapy 蜘蛛抓取一些数据后:

class Test_Spider(Spider):

name = "test"
def start_requests(self):
for i in range(900,902,1):
........
yield item

我将数据传递给管道对象,使用 SQLAlchemy::将其写入 SQLite 表

class SQLlitePipeline(object):

def __init__(self):
_engine = create_engine("sqlite:///data.db")
_connection = _engine.connect()
_metadata = MetaData()
_stack_items = Table("table1", _metadata,
Column("id", Integer, primary_key=True),
Column("detail_url", Text),
_metadata.create_all(_engine)
self.connection = _connection
self.stack_items = _stack_items

def process_item(self, item, spider):
is_valid = True

我希望能够将表名设置为变量,而不是像现在这样硬编码“table1”。如何做到这一点?

最佳答案

假设您通过命令行传递此参数(例如 -s table="table1"),定义一个 from_crawler 方法。

@classmethod
def from_crawler(cls, crawler):
# Here, you get whatever value was passed through the "table" parameter
settings = crawler.settings
table = settings.get('table')

# Instantiate the pipeline with your table
return cls(table)

def __init__(self, table):
_engine = create_engine("sqlite:///data.db")
_connection = _engine.connect()
_metadata = MetaData()
_stack_items = Table(table, _metadata,
Column("id", Integer, primary_key=True),
Column("detail_url", Text),
_metadata.create_all(_engine)
self.connection = _connection
self.stack_items = _stack_items

关于python - 如何将参数传递给 scrapy 管道对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41043970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com