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

埃德西尔

我正在尝试将已抓取的数据抓取存储到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()
阿桑·罗伊(Ahsan Roy)

您需要首先添加该类,ITEM_PIPELINES以便让沙哑的人知道我要使用此管道。

在您的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'您的实际项目名称。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

<ReferenceArrayInput />不能与我的自定义数据提供程序一起使用(棱镜)

为什么我的完成处理程序不能与我的 URLSession 一起使用?

为什么更改事件处理程序不能与我的代码一起使用?

连接池不能与用于SQL Server for Linux的ODBC Driver 13一起使用

SQL查询不能与之间的日期一起使用

sql <>不能与datetime一起使用

为什么我的数据库过滤器不能与Google数据存储一起使用?

为什么时间戳onEdit脚本不能与我的下拉菜单一起使用?

为什么我的for循环不能与我的数组一起正常工作?

像-不能将%% s%通配符与MySQL连接器一起使用

forEach() 不能与我的 HTML 标签一起正常工作,在 JSON 上循环

存储的选择器不能与each()一起正确使用

从脚本到PHP解释器的参数传递不能与引号一起使用,为什么?

将MySQL连接器与Python一起使用时SSL连接错误

将报告分组在一起(可能与SQL一起使用)?

Blazor将输入值绑定到oninput不能与onkeypress一起使用

我可以将View pager与视图一起使用吗(不能与片段一起使用)

为什么我的sql与Max函数一起使用时,它不能与Abs函数一起工作?

我的网络爬虫不能与 BeautifulSoup 一起使用

超级不能与类装饰器一起使用?

为什么我的$ templateCache设置不能与新的Angular路由器一起使用?

为什么我的 javascript 计算器不能与 base.html 一起使用?

为什么我的计数器不能与“ alternate”功能一起使用?

为什么我的 :nth-child() 选择器不能与 ::before 一起使用

为什么我的输入不能与数据库一起使用?

为什么我的视图模型没有绑定并且不能与映射插件一起使用foreach

Ajax / PHP / MySQL:将数据插入db不能与Ajax和PHP一起使用

SQL SELECT查询不能与参数一起使用,但可以与串联配合使用

将Webots连接器设备与ROS一起使用