将数据存储到SQL不能与我的sql连接器一起使用并且刮scrap

问题描述 投票:0回答:1

我试图将我的抓取数据存储到SQL数据库中,但是我的代码没有发送任何内容,而在运行时未提及任何错误。

我正在使用sql连接器,因为我无法安装MySQL-python。我的SQL数据库似乎运行良好,当我运行代码时,流量增加了KB / s。请在下面找到我的pipelines.py代码。

import mysql.connector
from mysql.connector import errorcode

class CleaningPipeline(object):
    ...

class DatabasePipeline(object):

    def _init_(self):
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = mysql.connector.connect(
            host = 'localhost',
            user = 'root',
            passwd = '********',
            database = 'lecturesinparis_db'
        )
        self.curr = self.conn.cursor()

    def create_table(self):
        self.curr.execute("""DROP TABLE IF EXISTS mdl""")
        self.curr.execute("""create table mdl(
                        title text,
                        location text,
                        startdatetime text,
                        lenght text,
                        description text,
                        )""")

    def process_item(self, item, spider):
        self.store_db(item)
        return item

    def store_db(self, item):
        self.curr.execute("""insert into mdl values (%s,%s,%s,%s,%s)""", (
            item['title'][0],
            item['location'][0],
            item['startdatetime'][0],
            item['lenght'][0],
            item['description'][0],
        ))
        self.conn.commit()
python sql scrapy mysql-python
1个回答
0
投票
在您的settings.py文件中,使用您的班级名称更新下面的行,如下所示。

# https://docs.scrapy.org/en/latest/topics/item-pipeline.html ITEM_PIPELINES = { 'projectname.pipelines.CleaningPipeline': 700, 'projectname.pipelines.DatabasePipeline': 800, }

700和800表示管道将按什么顺序处理数据,它可以是1-1000之间的任何整数。管道将根据此数字按顺序处理项目,因此具有700的管道将在具有800的管道之前处理数据。

注意:用您的实际项目名称替换'projectname.pipelines.CleaningPipeline'中的项目名称。

© www.soinside.com 2019 - 2024. All rights reserved.