开发者社区> 问答> 正文

scrapy数据导入mysql问题?报错

pipeline文件:


import MySQLdb

class Task1Pipeline(object):

    def process_item(self, item, spider):

        task1_db = spider.settings.get('task1_db')
        con = MySQLdb.connect(**task1_db)
        cur = con.cursor()
        sql = ("insert into person_in (client_name,quaification,get_method,certificate_n,office,class_hour,penalty_in)"
        "values(%s,%s,%s,%s,%s,%s,%s)")
        lis = (item['client_name'],item['quaification'],item['get_method'],item['certificate_n'],
               item['office'],item['class_hour'],item['penalty_in'])
        cur.execute(sql,lis)
        con.commit()
        cur.close()
        con.close()
        return item

settings.py中文件

BOT_NAME = 'task1'

SPIDER_MODULES = ['task1.spiders']
NEWSPIDER_MODULE = 'task1.spiders'

task1_db = {'db':'task1_db','user':'root','passwd':'fy',
            'host':'localhost','use_unicode':True,'charset':'utf8'}
ITEM_PIPELINES = {
    'task1.pipelines.Task1Pipeline':300,
}

报错:

TypeError: Connect() argument after ** must be a mapping, not NoneType

我就不懂了,按照别人源文件修改的,mysql已经建立task1_db这个数据库,为什么会报错怎么改??

展开
收起
爱吃鱼的程序员 2020-06-08 14:11:16 723 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
    defsetmodule(self,module,priority='project'):"""Storesettingsfromamodulewithagivenpriority.Thisisahelperfunctionthatcalls:meth:`~scrapy.settings.BaseSettings.set`foreverygloballydeclareduppercasevariableof``module``withtheprovided``priority``.:parammodule:themoduleorthepathofthemodule:typemodule:moduleobjectorstring:parampriority:thepriorityofthesettings.Shouldbeakeyof:attr:`~scrapy.settings.SETTINGS_PRIORITIES`oraninteger:typepriority:stringorint"""self._assert_mutability()ifisinstance(module,six.string_types):module=import_module(module)forkeyindir(module):ifkey.isupper():self.set(key,getattr(module,key),priority)

    所以,task1_db必须全大写

    虽然看不懂前面一大段,不过最后结论超给力~我马上试试看
    2020-06-08 14:11:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像