Scrapy + adbapi = AttributeError: 'module' object has no attribute 'DictCursor' Scrapy + adbapi = 属性错误:'module'对象没有属性'DictCursor'

3

我有一个CrawlSpider,我设置了一个item pipeline,并且我正在尝试通过MySQLdb进行持久化。我已经搜索了所有的地方,大部分的样例都至少有6个月的历史,都是以相同的方式使用adbapi。当我尝试使用相同的格式时,我会得到以下错误:

AttributeError: 'module' object has no attribute 'DictCursor'

我不知道我在这里做错了什么,但我对Python相当新,而且对scrapy非常新,所以很可能是我忽略了一些简单的东西。

from twisted.enterprise import adbapi
from scrapy import log

import MySQLdb.cursors

class InventoryPipeline(object):
    def __init__(self):
        self.pool = adbapi.ConnectionPool('MySQLdb',
                db='inventory',
                user='root',
                passwd='',
                cursorClass=MySQLdb.cursors.DictCursor,
                charset="utf8",
                use_unicode=True
            )


    def process_item(self, item, spider):
        query = self.pool.runInteraction(self._insert_record, item)
        query.addErrback(self._handle_error)
        return item


    def _insert_record(self, tx, item):
        tx.execute("select * from content where url = %s", (item['url']))
        result = tx.fetchone()
        if result:
            log.msg("url already in database", level=log.INFO)
        else:
            tx.execute("insert into content (url, title, link_content, main_content, header) values (%s, %s, %s, %s, %s)",
                (item['url'], item['title'], item['link_content'], item['main_content'], item['header']))
            log.msg("Item stored in db: %s" % item, level=log.INFO)
        return item


    def _handle_error(self, e):
        log.err(e)

顺便说一句 - 我也尝试过包括 "import MySQLdb" 和 "import MySQLdb.cursors",但都没有成功。 - richwalkup
这很奇怪。如果同时导入import MySQLdbimport MySQLdb.cursors会怎么样? - alecxe
服务器版本:5.5.15 MySQL社区服务器在我的MacBook Pro上。 - richwalkup
好的,但是我指的是Python模块本身 - mysqldb - alecxe
1
这个 MySQL-python 1.3.0 版本是从哪里来的?是从源代码 https://github.com/farcepest/MySQLdb1/tree/MySQLdb-1.3 来的吗?在 PyPi 上我只看到了 1.2.4 版本。 - paul trmbrth
显示剩余2条评论
1个回答

0

我也遇到了这个问题,不过我解决了。

你可以试试:

从MySQLdb.cursors导入DictCursor

并将cursorClass更改为“cursorClass = DictCursor”

它在我的电脑上有效,希望它也能帮到您和其他人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接