将数据存储到 SQL 不适用于我的 sql 连接器和 scrapy
Storing data to SQL not working with my sql connector and scrapy
我正在尝试将我用 scrapy 抓取的数据存储到 SQL 数据库,但我的代码没有发送任何东西,而且在 运行ned 时没有提到错误。
我正在使用我的 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()
您需要先在 ITEM_PIPELINES
中添加 class 让 scrapy 知道我想使用这个管道。
在您的 settings.py
文件中用您的 class 名称更新下面的行,如下所示。
# 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'
中的项目名称替换为您实际的项目名称。
我正在尝试将我用 scrapy 抓取的数据存储到 SQL 数据库,但我的代码没有发送任何东西,而且在 运行ned 时没有提到错误。
我正在使用我的 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()
您需要先在 ITEM_PIPELINES
中添加 class 让 scrapy 知道我想使用这个管道。
在您的 settings.py
文件中用您的 class 名称更新下面的行,如下所示。
# 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'
中的项目名称替换为您实际的项目名称。