在使用可抓取的蜘蛛抓取一些数据之后:
class Test_Spider(Spider):
name = "test"
def start_requests(self):
for i in range(900,902,1):
........
yield item
Run Code Online (Sandbox Code Playgroud)
我将数据传递给使用sqlalchemy写入sqllite表的管道对象:
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
Run Code Online (Sandbox Code Playgroud)
我希望能够将表名设置为变量,而不是像现在这样被硬编码(“ table1”)。如何才能做到这一点?