我正在使用Scrapy Spider抓取一些数据,然后使用sqlalchemy和psycopg2将其插入到我的postgresql数据库中。该代码可在Windows10计算机上运行,但是,在基于Linux的系统(Raspberry Pi,Debian Stretch)上运行它时,出现错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'dict'
有人知道在Windows上运行时是什么使它不起作用吗?
pipeline.py
from sqlalchemy import create_engine, Table, MetaData
from spider.models import db_connect, load_table
from sqlalchemy.dialects.postgresql import insert
class PgPipeline(object):
def __init__(self):
"""
Initializes database connection and sessionmaker.
Reflects the spider_rent table.
"""
engine = db_connect()
self.RentDB = load_table(engine)
self.conn = engine.connect()
def process_item(self, item, spider):
"""Save listings in the database.
This method is called for every item pipeline component.
"""
stmt = insert(self.RentDB).values(item)
stmt_on_conflict = stmt.on_conflict_do_update(
index_elements=['hash_id'],
set_=dict(last_seen=item['last_seen'])
)
self.conn.execute(stmt_on_conflict)
return item
def close_spider(self, spider):
self.conn.close()
model.py
from sqlalchemy import create_engine, Column, Integer, MetaData, Table
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.engine.url import URL
import spider.settings as settings
metadata = MetaData()
def db_connect():
"""
Performs database connection using database settings from settings.py.
Returns sqlalchemy engine instance
"""
return create_engine(URL(**settings.DATABASE))
def load_table(engine):
"""
Reflects the spider_rent table in the DB
"""
return Table('spider_rent', metadata, autoload=True, autoload_with=engine)
编辑:
我注意到在Debian系统而不是Windows上抛出该错误的原因是因为PostgreSQL列设置为TEXT。
在Windows上有效,因为在Debian上使用字典时字典使用''来表示文本。当我将数据库中的列类型更改为JSON时,Debian系统正常工作-但是我的Windows系统停止工作。
有人知道怎么算吗?