使用SqlAlchemy将数据存入db,object不可订阅

Saving data into db using SqlAlchemy, object is not subscriptable

我正在尝试将一些数据插入数据库,不幸的是它失败了并且没有保存,我怀疑我的数据结构有误。在尝试保存 "print(title, link, date)" 之前,数据在 process_item 中打印得很好(1 个标题,link 和每个 object 的日期),但是保存失败。标题、link 和日期各包含 1 个字符串...

感谢您的帮助


错误:

"Traceback (most recent call last):
  File "spider.py", line 63, in <module>
    presstv = spider_html(presstv_url, presstv_extract_item, presstv_xpath, presstv_pipeline)
  File "spider.py", line 58, in spider_html
    pipeline.process_item(extract_function(element), None)
  File "/Users/dav/Projects/python/news/pipeline.py", line 76, in process_item
    if session.query(Presstv).filter_by(link=item['link']) == None:
TypeError: 'Presstv' object is not subscriptable"



代码

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from models import Nordfront, Presstv, db_connect, create_presstv_table
import json



class PresstvPipeline(object):
    """Pipeline for storing scraped items in the database"""
    def __init__(self):
        """
        Initializes database connection and sessionmaker.
        Creates deals table.
        """
        engine = db_connect()
        create_presstv_table(engine)
        self.Session = sessionmaker(bind=engine)


    def process_item(self, items, spider):

        session = self.Session()


        for title, link, date in zip(items['title'], items['link'], items['date']):

            print(title, link, date)
            item = Presstv(title = title, link = link, date = date)


            if session.query(Presstv).filter_by(link=item['link']) == None:
                try:
                    session.add(item)
                    session.commit()
                    logger.info('Item saved')
                except:
                    session.rollback()
                    raise
                finally:
                    session.close()

                return item



型号:

class Presstv(DeclarativeBase):
    """Sqlalchemy deals model"""
    __tablename__ = "presstv"

    id = Column(Integer, primary_key=True)
    title = Column('title', String)
    description = Column('description', String, nullable=True)
    link = Column('link', String, unique=True)
    date = Column('date', String, nullable=True)
    created_at = Column('created_at', DateTime, default=_get_date)

你应该使用:

if session.query(Presstv).filter_by(link=item.link) == None:

因为 item 现在是 SQLAlchemy 的一个对象。这可能是因为您在此之前使用了 items['link'] 几行,但 item 现在是 class 的一个实例,因此您应该使用 .link.[=15 访问其值=]